变量名必须在for循环中更改


php Variable name must change in for loop

在我的表单上,我有一个选择表。我将其放入for循环中,以便每次选择的名称更改为option0, option1等。然后在表单的动作页面上,我需要获得所选每个选项的信息,例如$_POST['option0'], $_POST['option1']等,将其输入到表中。我不能让动作页工作,我做错了什么?

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>";
    }

第2页

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

您正在调用您的可选择选项集$opt = "<select name='option". $x . "'>";

所以Page2应该寻找名为'option0'的东西…'option13' ' like this

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

你试过打印post数组吗?使用print_r:

print_r($_POST);

我不相信这个帖子有一个叫做"面试官...."的东西什么。

此外,如果您希望混合使用文本和变量,请使用

$_POST["interviewer {$x}"]

去掉单引号和圆点。单引号不能计算变量:

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