1

I am pretty new to PHP and I have these two Radio Buttons.

<input type="radio" name="gender" value="female" checked = "checked" /> Female
<input type="radio" name="gender" value="male" />   

I'm trying to see which one was selected by doing something like this...

$radio = $_POST["gender"];

How would I know which one was selected?

4
  • 1
    Only the selected one is sent to the server. Commented Dec 9, 2013 at 17:38
  • 1
    Thank you, I didn't know how it was going to send. Commented Dec 9, 2013 at 17:43
  • 1
    $radio has value of selected radio button. Commented Dec 9, 2013 at 17:44
  • thanks for this question, it was exactly what I was looking for - i know these type of comments generally are frowned upon, but keep up the good work! Commented Oct 19, 2016 at 14:54

2 Answers 2

2

Only the selected checkbox will be sent to your server. To check in your code which one was selected, you can use:

if($_POST['gender'] === 'female') {
    //Female selected
}
else if($_POST['gender'] === 'male') {
    //Male selected
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is what I was looking for, I don't know why I was down voted though...
1

The variable $radio will have the value of the <input> checked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.