Jump to content

Forms - Radios and Check Boxes


Recommended Posts

Alright, so I learned about check boxes and radio buttons a long time ago from w3schools.com. But I never used them in forms. I'm currently developing a site that is going to have a nice account settings page for the user that will use both. Let's say I had this little bit of HTML:

Gender: <input type="radio" name="gender" value="male" /> Male <input type="radio" name="gender" value="female" /> Female<br />Activate DST Edit <input type="checkbox" name="dst" value="yes" /><br />

In order to get the PHP to read those, would it just use the regular $_POST method?

$gender = $_POST['gender'];$dst = $_POST['dst'];

Would that do it? Or would the fact that both radio buttons are named gender interfere? And what will the $dst variable be if the check box is left unchecked?

Link to comment
Share on other sites

Yes.In the first case $_POST['gender'] will return the selected value.In the second case, $_POST['dst'] will return "yes" if selected and an empty string if it's not selected.I usually use strlen() to evaluate checkbox values:

if(strlen($_POST['dst'])) {// Some code here} else {// Some other code here}

Though checking for the value also works:

if($_POST['dst'] == "yes") {// Some code here} else {// Some other code here}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...