How to send value attribute from radio button in PHP [closed]
Asked Answered
G

6

20

I am struggling with sending the value of a radiobutton to an email.

I have coded 2 radiobuttons, where I have set the first on to be default checked.

The form and values work, however the radio button value is not submitted.

Any wise words?

Goy answered 7/2, 2013 at 9:34 Comment(1)
Without your code, all we can do is copy and paste from the HTML spec.Agriculturist
D
22

When you select a radio button and click on a submit button, you need to handle the submission of any selected values in your php code using $_POST[] For example: if your radio button is:

<input type="radio" name="rdb" value="male"/>

then in your php code you need to use:

$rdb_value = $_POST['rdb'];
Devilment answered 23/9, 2013 at 9:50 Comment(1)
thanks, yes it was the value attribute ... I was teaching myself from scratch, but your answer actually helped me solve several other problems too ... so thank you very much :-)Douville
E
5

Check whether you have put name="your_radio" where you have inserted radio tag

if you have done this then check your php code. Use isset()

e.g.

   if(isset($_POST['submit']))
   {
    /*other variables*/
    $radio_value = $_POST["your_radio"];
   }

If you have done this as well then we need to look through your codes

Eddi answered 7/2, 2013 at 10:2 Comment(2)
should I use same name for radio buttons in html?Shadbush
yes and the value will be either true or falseEddi
P
2

The radio buttons are sent on form submit when they are checked only...

use isset() if true then its checked otherwise its not

Parette answered 7/2, 2013 at 9:38 Comment(0)
T
2

When you select a radio button and click on a submit button, you need to handle the submission of any selected values in your php code using $_POST[]
For example:
if your radio button is:

<input type="radio" name="rdb" value="male"/>

then in your php code you need to use:

$rdb_value = $_POST['rdb'];
Treasonable answered 7/2, 2013 at 9:45 Comment(0)
S
2

Radio buttons have another attribute - checked or unchecked. You need to set which button was selected by the user, so you have to write PHP code inside the HTML with these values - checked or unchecked. Here's one way to do it:

The PHP code:

<?PHP
    $male_status = 'unchecked';
    $female_status = 'unchecked';

    if (isset($_POST['Submit1'])) {
         $selected_radio = $_POST['gender'];

         if ($selected_radio == 'male') {
                $male_status = 'checked';
          }else if ($selected_radio == 'female') {
                $female_status = 'checked';
          }
    }
?>

The HTML FORM code:

<FORM name ="form1" method ="post" action ="radioButton.php">
   <Input type = 'Radio' Name ='gender' value= 'male'
   <?PHP print $male_status; ?>
   >Male
   <Input type = 'Radio' Name ='gender' value= 'female' 
   <?PHP print $female_status; ?>
   >Female
   <P>
   <Input type = "Submit" Name = "Submit1" VALUE = "Select a Radio Button">
</FORM>
Stingy answered 7/2, 2013 at 9:47 Comment(1)
There shouldn't be a space in = =.Winson
C
1

Should be :

HTML :

<form method="post" action="">
    <input id="name" name="name" type="text" size="40"/>
    <input type="radio" name="radio" value="test"/>Test
    <input type="submit" name="submit" value="submit"/>
</form>

PHP Code :

if(isset($_POST['submit']))
{

    echo $radio_value = $_POST["radio"];
}
Coomer answered 7/2, 2013 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.