Suzie
Suzie

Reputation: 45

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>

Upvotes: 0

Views: 129

Answers (4)

dwndmr
dwndmr

Reputation: 36

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

Upvotes: 2

Raven
Raven

Reputation: 21

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/

Upvotes: 0

Zain Farooq
Zain Farooq

Reputation: 2964

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'];
 ?>

Upvotes: 0

Kalabalik
Kalabalik

Reputation: 157

Start to putting in a form instead of a div, then you name the buttons with the same name... thats it, if you got the quiz.php right...

Upvotes: 0

Related Questions