Jump to content

[SOLVED] Validating radio buttons


micmania1

Recommended Posts

Method 1

 

Set one of the buttons "checked" by default so a value is always posted

 

 

Method 2

 

If neither selected then no value posted

 

<?php
if (isset($_POST['action'])) {

    if (!isset($_POST['myrb']) ) {
        echo "Please select Yes or No" ;
    }
    else {
        echo 'You selected ' . $_POST['myrb'] ;
    }
}
?>
<form method='post'>
Yes <input type='radio' name='myrb' value='Yes'><br/>
No <input type='radio' name='myrb' value='No'><br/> 
<input type='submit' name='action' value='Submit'>
</form> 

 

Method 3

Have hidden field before the radio buttons on the form with same name then it's value is sent if no button selected

 

<?php
if (isset($_POST['action'])) {

    if ($_POST['myrb'] == 0 ) {
        echo "Please select Yes or No" ;
    }
    else {
        echo 'You selected ' . $_POST['myrb'] ;
    }
}
?>
<form method='post'>
<input type='hidden' name='myrb' value='0'>
Yes <input type='radio' name='myrb' value='Yes'><br/>
No <input type='radio' name='myrb' value='No'><br/> 
<input type='submit' name='action' value='Submit'>
</form> 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.