Reputation: 95
I have a form that has radio inputs that have a dynamically generated name like below:
<input type="number" name="<?php echo date("Y")-4 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-4 ?>">
<input type="number" name="<?php echo date("Y")-3 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-3 ?>">
<input type="number" name="<?php echo date("Y")-2 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-2 ?>">
<input type="number" name="<?php echo date("Y")-1 ?>brand%gross" placeholder="Gross Sales % for <?php echo date("Y")-1 ?>">
Then the form is processed and creates a dynamic table with all the values the user inputs that is then emailed to me. My question is how do I get dynamically generated radios to output to the table that is sent? I know the below isn't correct but just to give you a better look at what I'm trying to do:
if( isset($_POST) ){
${ echo date("Y")-4 } = $_POST['{ echo date("Y")-4 }];
Any help is greatly appreciated!
Upvotes: 0
Views: 4627
Reputation: 71384
By have a string (the output of date()
) from which you subtract and integer, you end up casting the result as an integer, not a string. You are probably better off making the string correctly to begin with and getting rid of the integer arithmetic issue.
I would suggest working with DateTime objects. Here's how you might output you input fields.
<?php
$current_date = new DateTime();
while($i = 1; $i <= 4; $i++) {
$date_int = new DateInterval('P' . (string)$i . 'Y');
$temp_date = $current_date->sub($date_int);
$date_string = $temp_date->format('Y');
?>
<input type="number" name="<?php echo $date_string; ?>brand%gross" placeholder="Gross Sales % for <?php echo $date_string; ?>" />
<?php
} // end while
?>
When processing the $_POST
you can do similar:
<?php
$current_date = new DateTime();
$year_array = array();
while($i = 1; $i <= 4; $i++) {
$date_int = new DateInterval('P' . (string)$i . 'Y');
$temp_date = $current_date->sub($date_int);
$date_string = $temp_date->format('Y');
if (!empty($_POST[$date_string. 'brand%gross'])) {
$year_array[$date_string] = $_POST[$date_string . 'brand%gross'];
}
} // end while
?>
Note that I use an array to store your data indexed by year string , as you can't have a variable name that starts with a number (i.e. $2013nrand%gross
is not valid).
I would also STRONGLY suggest using array access notation in your inputs to simply things.
If you made each input like this:
<input type="number" name="year_brand_gross[<?php echo $date_string; ?>]" placeholder="Gross Sales % for <?php echo $date_string; ?>" />
Then the names of year_brand_gross[2013]
and such would automatically get populated as an array into $_POST['year_brand_gross']
, eliminating the need to loop through the POST input.
Instead you could set this to a variable like this
if(!empty($_POST['year_brand_gross']) {
$year_array = $_POST['year_brand_gross'];
}
For PHP < 5.3.0 you can use alternate method to generate the year strings:
$current_date = new DateTime();
for ($i = 1; $i <=4; $i++) {
$current_date->modify('-1 year');
$date_string = $current_date->format('Y');
// other code here
}
Note that, as shown, this will alter the value of $current_date
with each iteratoin, which differs from the first solution.
Upvotes: 1
Reputation: 809
If you want to set a varible content inside $_POST, well... Use a variable.
$foo = date("Y")-4;
$_POST['str'] = $foo;
So... the thing is , you want to use a variable variable name that might go like this:
$foo = date("Y")-4;
$$foo = $_POST[$foo];
Got it?
EDIT 1:
So that the variable won't start with a number:
$bar = "dt_";
$foo = date("Y")-4;
${$bar.$foo} = $_POST[$foo];
Upvotes: 2
Reputation: 24645
First off you want to update you html so that the names of the fields
<input type="number" name="year[<?= date("Y")-4 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-4 ?>">
<input type="number" name="year[<?= date("Y")-3 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-3 ?>">
<input type="number" name="year[<?= date("Y")-2 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-2 ?>">
<input type="number" name="year[<?= date("Y")-1 ?>brand%gross]" placeholder="Gross Sales % for <?= date("Y")-1 ?>">
In your PHP you will be able to access the elements by there keys
$val2010 = $_POST["year"]["2010brand%gross"];
Upvotes: 0