douxun1934 2016-11-15 10:18
浏览 67
已采纳

返回循环未定义索引错误中的几个下拉表单的值

I have created a HTML form inside a PHP loop but I am having trouble returning the values of the dropdown lists after clicking my submit button. The problem is that it is returning a value for $dropdown0, but not for any other $dropdown. In this example there are 3 questions, so it should be returning $dropdown0, $dropdown1 and $dropdown2, yet when I press submit, it gives an error saying:

Notice: Undefined index: dropdown1 in C:\xampp\htdocs\website\playQuiz.php on line 54

Notice: Undefined index: dropdown2 in C:\xampp\htdocs\website\playQuiz.php on line 55

for($x=0; $x<=$noQuestions-1; $x++){
    $type = "dropdown".$x;
    echo "<table>";
    echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
    echo "<form method=\"post\">";
    echo "<Select class=\"form-control\" type=\"text\" name=$type required>";
    echo "<option value=\"1\">".$optionData[$x][0]."</option>";
    echo "<option value=\"2\">".$optionData[$x][1]."</option>";
    echo "<option value=\"3\">".$optionData[$x][2]."</option>";
    echo "<option value=\"4\">".$optionData[$x][3]."</option>";
    echo "</select>";
    echo "</form>";
    echo "</table>";
}

The code for printing the values of the drop down lists using a button called 'submit':

if (isset($_REQUEST['submit'])){
    extract($_REQUEST);

    echo $_POST['dropdown0'];
    echo $_POST['dropdown1'];
    echo $_POST['dropdown2'];
}
  • 写回答

1条回答 默认 最新

  • dongxieting9623 2016-11-15 10:27
    关注

    that is because you have built 3 forms inside that loop, so when you click the submit button it sends the first form only, you need to get the form element outside the loop

    echo "<form method=\"post\">"; // << here
    For($x=0; $x<=$noQuestions-1; $x++){
                    $type = "dropdown".$x;
                    echo "<table>";
                    echo "<tr>"."Question ".($x+1).": ".$question[$x]."</tr>";
                    echo "<Select class=\"form-control\" type=\"text\" name=$type required>";
                    echo "<option value=\"1\">".$optionData[$x][0]."</option>";
                    echo "<option value=\"2\">".$optionData[$x][1]."</option>";
                    echo "<option value=\"3\">".$optionData[$x][2]."</option>";
                    echo "<option value=\"4\">".$optionData[$x][3]."</option>";
                    echo "</select>";
    
                    echo "</table>";
                }
    echo "</form>";  // << here
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)