Search code examples
phppostechodynamic-variables

Dynamic variable echo inside $_POST['']


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!


Solution

  • 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.