简体   繁体   English

获取单选按钮值php

[英]getting Radio button value php

Hi i cant seem to get the value of the radio button generated by the loop statement. 嗨,我似乎无法获得loop语句生成的单选按钮的值。 The $_POST method doesn't work even there is corresponding radio button name. 即使有相应的单选按钮名称,$ _ POST方法也不起作用。

 <?php
$servername = "127.0.0.1";
$username = "root";
$password = "";
$dbname = "test";
$i = null;
$conn = new mysqli($servername, $username, $password, $dbname);

for ($t=0;$t<4;$t++){}
$sql = "SELECT * FROM questions LIMIT $t";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    $i = 0; 
        echo  " <form action='radio.php' method='post'><table>";
        echo "<tr>
        <th>Questions</th>
        <th>Answers</th>     </tr> <tr>" ;
    while($row = $result->fetch_assoc()) {
    $ans=array($row['1'],$row['2'],$row['3']);
    shuffle($ans);
    echo $row['question']."<br>";
        foreach ($ans as $choice) {
           echo "<input type='radio' name='radio$i' value = '$choice'>".$choice."<br>";
    } unset($choice);
        echo " </tr>";$i++;
    }


        echo "</table> <input type = 'submit' value = 'finished'></form><br>";
echo "";
    } else {
    echo "0 results";
}
$conn->close();

 ?>

It always results in 它总是导致
Notice: Undefined index: radio0 in C:\\xampp\\htdocs\\Bootstrap\\radio.php here is an example output Even tho the index radio0 exists in the element of the html. 注意:未定义的索引:C:\\ xampp \\ htdocs \\ Bootstrap \\ radio.php中的radio0 此处是示例输出,即使html元素中存在索引radio0

Let's go through the code little by little 让我们一点一点地看一下代码

for ($t=0;$t<4;$t++){}

This is an empty for loop. 这是一个空的for循环。 The brackets { and } should include the actions of the loop. 方括号{}应包含循环的动作。 Since they're empty, nothing is happening in this loop 由于它们为空,因此此循环中没有任何反应

$sql = "SELECT * FROM questions LIMIT $t";

You can't use the $t variable here, because it is out of scope. 您不能在此处使用$ t变量,因为它超出了范围。 Even if you could, I doubt this is what you really want, since LIMIT merely limits the number of responses you can get. 即使您可以,但我仍然怀疑这是您真正想要的,因为LIMIT仅限制了​​您可以获得的响应数量。

$result = $conn->query($sql);

You havn't defined a connection object (assuming you've provided all your code). 您尚未定义连接对象(假设您已提供所有代码)。

 $i = 0; echo  " <form action='radio.php' method='post'><table>";

It is really terrible practice to define these things on the same line (although it is possible). 在同一行上定义这些东西确实是很糟糕的做法(尽管有可能)。

<th>Answers</th>     </tr> <tr>" ;while($row = $result->fetch_assoc()) {

Again, terribly practice - it is very difficult to read. 再次,非常实践-这很难阅读。

$ans=array($row['1'],$row['2'],$row['3']);

You should most likely be using integer indexes instead of strings. 您很可能应该使用整数索引而不是字符串。 Furthermore remember that arrays are 0 -indexed. 此外,请记住,数组是0索引的。

foreach ($ans as $choice) {echo "<input type='radio' name='radio$i' value = '$choice'>".$choice."<br>";
 } unset($choice);
              echo " </tr>";$i++;}

You havn't defined any $i variable in your loop, so you can't use the $i variable 您尚未在循环中定义任何$i变量,因此无法使用$i变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM