简体   繁体   English

具有一种形式的两个不同的单选按钮,并使用php发布

[英]Two different radio button with one form and post it using php

I am stuck with this code, trying to post 2 different radio group my problem is I want to make the user to check 1 radio button for ervery 4 results , I mean how can I know when user has choose other, with this code I always can choose more than one radio , any help? 我坚持使用此代码,尝试发布2个不同的单选组我的问题是我想让用户检查1个单选按钮以获取4个结果,我的意思是我如何才能知道用户何时选择了其他单选可以选择多个收音机,有什么帮助吗?

                $sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
                $result2 = mysqli_query($conn,$sql);

              echo "<form action='' method='post'>";
                  while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {

                    echo "<div align='center'>
                    <table class='demo'  dir='rtl'>
                      <tbody>
                      <tr>
                        <td>".$row2['ojs']."</td>
                        <td> </td>
                      </tr>
                      <tr>
                        <td>".$row2['txt1']."</td>
                        <td><input type='radio' name='chk1' value='1' >
                      </tr>
                      <tr>
                        <td>".$row2['txt2']."</td>
                        <td><input type='radio' name='chk2' value='2' >
                      </tr>
                      <tr>
                        <td>".$row2['txt3']."</td>
                        <td><input type='radio' name='chk3' value='3' >
                      </tr>
                      <tr>
                        <td>".$row2['txt4']."</td>
                        <td><input type='radio' name='chk4' value='4' >
                      </tr>
                      </tbody>
                    </table><br>
                    </div>
                    ";
                  }
                  echo "<input type='submit' name='submit'>
                  </form>";
                  if (isset($_POST['submit'])) {
                    $chk1 = $_POST['chk1'];
                    $chk2 = $_POST['chk2'];
                    $chk3 = $_POST['chk3'];
                    $chk4 = $_POST['chk4'];
                    echo $chk4."|".$chk3."|".$chk2."|".$chk1;

//insert into mysql , just the Selected radio button for each different question
$insQry = "insert into tbl2 (id,sel1,sel2,sel3,sel4) VALUES('$chk1','$chk2','$chk3','$chk4')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
                  }

You need to set the name of the entire adio input group to one name. 您需要将整个adio输入组的名称设置为一个名称。 Since you have them set to four different names, it treats them as as four different entities. 由于您将它们设置为四个不同的名称,因此将它们视为四个不同的实体。

You need to give them the same name then get it in PHP like 您需要给它们起相同的名称,然后在PHP中获得它,例如

 $sql = "SELECT * FROM tbl1 WHERE object = '".$obj."' LIMIT 5 ";
            $result2 = mysqli_query($conn,$sql);

          echo "<form action='' method='post'>";
              while ($row2 = mysqli_fetch_array($result2,MYSQLI_ASSOC)) {

                echo "<div align='center'>
                <table class='demo'  dir='rtl'>
                  <tbody>
                  <tr>
                    <td>".$row2['ojs']."</td>
                    <td> </td>
                  </tr>
                  <tr>
                    <td>".$row2['txt1']."</td>
                    <td><input type='radio' name='chk' value='1' >
                  </tr>
                  <tr>
                    <td>".$row2['txt2']."</td>
                    <td><input type='radio' name='chk' value='2' >
                  </tr>
                  <tr>
                    <td>".$row2['txt3']."</td>
                    <td><input type='radio' name='chk' value='3' >
                  </tr>
                  <tr>
                    <td>".$row2['txt4']."</td>
                    <td><input type='radio' name='chk' value='4' >
                  </tr>
                  </tbody>
                </table><br>
                </div>
                ";
              }
              echo "<input type='submit' name='submit'>
              </form>";
              if (isset($_POST['submit'])) {
                $chk = $_POST['chk'];
                echo $chk;

Then simply set the adjacent one in the database to selected number and others to 0 然后只需将数据库中的相邻一个设置为选定的数字,其他设置为0

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

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