简体   繁体   中英

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. 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.

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:

$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

EDIT: as mentioned in the comments your div should be an form

开始以表格形式而不是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
  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? 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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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