简体   繁体   中英

Send dynamic array in form via $_POST to html email

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!

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.

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM