php Variable name must change in for loop

51 Views Asked by At

On my form I've got a select from a table. I've put it in a for loop so that each time the name of the select changes to option0, option1, etc. Then on the action page of the form I need to get the info for each option selected, eg $_POST['option0'], $_POST['option1'], etc to input it into a table. I can't get the action page to work, what am I doing wrong?

PAGE1

for ($x = 0; $x <= 14; $x++) { 
    $get2a = mysqli_query($con,"SELECT * FROM table");
    $opt = "<select name='interviewer". $x . "'>";
    while($row2a = mysqli_fetch_array($get2a)) {
        $intvID = $row2a['intvID'];
        $opt .= "<option value = '";
        $opt .= $row2a['intvID'];
        $opt .= "'>";
        $opt .= $row2a['intvID'];
        $opt .= "</option>";
    }

PAGE 2

for ($y = 0; $y <= 14; $y++) {
    echo $_POST['interviewer . $y . '];
}
2

There are 2 best solutions below

1
On BEST ANSWER

You are calling your selectable options sets $opt = "<select name='option". $x . "'>";

So Page2 should be looking for things names 'option0' ... 'option13'` like this

for ($y = 0; $y <= 14; $y++) {
    echo $_POST['option' . $y];    <-- notice slight syntax change also
}   
0
On

Have you tried printing the post array? Use print_r:

print_r($_POST);

I don't believe the post has something that is named 'interviewer .... something.

besides, if you wish to mix text and variables, use

$_POST["interviewer {$x}"]

remove the single quotes and the dots. Single quotes do not evaluate variables:

$_POST['interviewer . $y . ']; -> wrong