简体   繁体   English

如何使用php和mysql验证单选按钮?

[英]How to validate radio buttons using php and mysql?

I have this set of code for radio buttons and it selects all the three options instead of one at a time. 我为单选按钮设置了这组代码,它选择了所有三个选项,而不是一次选择一个。 And how to connect it to MySQL database for proceeding further with the selected option on the radio button. 以及如何将其连接到MySQL数据库以进一步处理单选按钮上的选定选项。 Thanks in advance :) 提前致谢 :)

    <div class="radio" method="post" action="quiz.php">
    <label><input type="radio" name="GK" value="GK">GK</label><br><br>
    <label><input type="radio" name="OP" value="OP">Our Pasts</label><br><br>
    <label><input type="radio" name="D" value="D">Discovery</label><br><br><br>
    <button type="submit" class="btn btn-primary">Submit</button>

You have to use the same Name for the Radio options. 您必须对Radio选项使用相同的Name。

Also the button needs a name and a value: 此外,按钮还需要一个名称和一个值:

<form class="radio" method="post" action="quiz.php">
    <label><input type="radio" name="RADIO" value="GK">GK</label><br><br>
    <label><input type="radio" name="RADIO" value="OP">Our Pasts</label><br><br>
    <label><input type="radio" name="RADIO" value="D">Discovery</label><br><br><br>
  <button name="SubmitForm" type="submit" value='save' class="btn btn-primary">Submit</button>
</form>

After that you can $_POST the values: 之后,您可以$ _POST值:

$ButtonSaved = $_POST["SubmitForm"];
$RadioValue = $_POST["RADIO"];

if($ButtonSaved == "save")
{
   //do stuff with database
}

Further Information to interact with your MySQL Database in the manuel: http://php.net/manual/de/book.mysqli.php 与手册中的MySQL数据库进行交互的更多信息: http : //php.net/manual/de/book.mysqli.php

EDIT: as mentioned in the comments your div should be an form 编辑:如评论中所述,您的div应该是表格

开始以表格形式而不是div形式放置,然后使用相同的名称命名按钮...就这样,如果您对quiz.php的理解正确的话...

You have different names for all radio buttons. 所有单选按钮的名称都不同。 Set one name for all radio buttons which belong to same group. 为属于同一组的所有单选按钮设置一个名称。

<label><input type="radio" name="SomeName" value="GK">GK</label><br><br>
<label><input type="radio" name="SomeName" value="OP">Our Pasts</label><br><br>
<label><input type="radio" name="SomeName" value="D">Discovery</label><br><br><br>
<input type="submit" name='submit' class="btn btn-primary" value="Submit">

In php 在PHP中

<?php
  if(isset($_POST['submit'])
    $radioValue = $_POST['SomeName'];
 ?>

Since I can't comment, going with your question it seems that you can choose all 3 buttons instead one at a time? 既然我无法发表评论,那么您的问题似乎可以一次选择所有3个按钮,而不是一次选择一个? If that's the point then you should change your input type to: 如果那是重点,那么您应该将输入类型更改为:

<input type="checkbox" name="choices" value="GK" /><label for="GK">GK</label>
<input type="checkbox" name="choices" value="OP" /><label for="OP">Our Pasts</label>
<input type="checkbox" name="choices" value="D" /><label for="D">Discovery</label>

Then see here how to handle data via PHP: https://www.formget.com/php-checkbox/ 然后在这里查看如何通过PHP处理数据: https//www.formget.com/php-checkbox/

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

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