0

This title might not describe my question too well but I was unsure how to name this post... Anyways, I have a form that has dynamically generated input boxes that pulls the last 4 years with the following:

<?php
    $current_date = new DateTime();
    for ($i = 1; $i <= 4; $i++) {
        $current_date->modify('-1 year');
        $date_string = $current_date->format('Y')
?>

<fieldset name="gross_sales">
    <input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales for <?php echo $date_string; ?>">
</fieldset>

<?php
} // end while
?>

And once the user clicks submit the form data is processed via my process.php file that contains the following:

$year_brand_gross[1] = $_POST['year_brand_gross'][1];
$year_brand_gross[2] = $_POST['year_brand_gross'][2];
$year_brand_gross[3] = $_POST['year_brand_gross'][3];
$year_brand_gross[4] = $_POST['year_brand_gross'][4];

Now I'm pretty sure the above part is not right. So this is my question... How would I get the info from these inputs into my email that's sent since their created by an array and not "actually" there. Here's a stripped down version of my html email that's sent which I'm pretty sure is also wrong since the above code is incorrect:

<table>
    <tr>
        <td>Gross Sales:</td>
    </tr>
    <tr>
        <td>{$year_brand_gross[1]}</td>
        <td>{$year_brand_gross[2]}</td>
        <td>{$year_brand_gross[3]}</td>
        <td>{$year_brand_gross[4]}</td>
    </tr>
</table>

Any help is greatly appreciated!

3
  • Can we see the whole code? That might help. Commented Jun 14, 2013 at 19:20
  • $_POST['year_brand_gross'][1] should be changed to $_POST['year_brand_gross'][2012] Commented Jun 14, 2013 at 19:22
  • Then the code would need updated every year. We did it this way so that the code would not need updating. And @tntu the whole code is about 3,000 lines and semi-confidential. I have posted all the necessary code. Commented Jun 14, 2013 at 20:28

2 Answers 2

1

Your form would actually look like

<input type="number" name="year_brand_gross[2012]" ... />
<input type="number" name="year_brand_gross[2011]" ... />
<input type="number" name="year_brand_gross[2010]" ... />
etc...

That means you need to use

$_POST['year_brand_gross'][2012]
$_POST['year_brand_gross'][2011]
$_POST['year_brand_gross'][2010]
etc...

on the server.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey Marc, thanks for your input! But I'm afraid this won't work. We used an array that dynamically made an input for the last 4 years so that we wouldn't have to update the code every year. So this year it would show 2013-2009 whereas next year it would show 2014-2010...
I understand what the php code "prints" when a user views it. The problem is since we're using dynamically generated inputs I'm not sure what goes where the # signs are in the following since it can't actually be a year: $year_brand_gross# = $_POST['year_brand-gross'][#]
so do foreach($_POST['year_brand_gross'] as $year => $value). you'll get the embedded year and its associated value, without caring WHAT years they are
Ok so I can replace the following: $year_brand_gross[1] = $_POST['year_brand_gross'][1]; $year_brand_gross[2] = $_POST['year_brand_gross'][2]; $year_brand_gross[3] = $_POST['year_brand_gross'][3]; $year_brand_gross[4] = $_POST['year_brand_gross'][4]; with what you posted above?
0
foreach($_POST['year_brand_gross'] AS $yeah => $value) {
  // use $year and $value variables to do whatever
  // this code will execute once for each values in $_POST['year_brand_gross'].
  // note: print_r($_POST);
  // $_POST is an array, same for $_GET and so on
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.