Jump to content


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


Radio Button

Recommended Posts

I have two radio buttons made.

Male | Female

I want to be able to pass this over to another page, I've already posted the tags into the PHP code, but how do I configure PHP to knowing which button is selected? I want to put the selected button in the database, under "gender" of course. Could someone help me out?

Thanks in advance :]

Share this post

Link to post
Share on other sites
I am assuming that you have already posted the data to the script that you want to parse the information and that you already know how to connect to your db? Use the following code to input the data into the db:

i have used gender as the name of your radio button.

[code]//post the data from your form
$gender = $_POST['gender'];

//insert into db
$sql = "INSERT INTO your_db_name (gender)
    VALUES ('$gender')";
$result = mysql_query($sql);

//you can check if the data has been inserted properly
  echo 'There was a problem inserting your data into the databse please try again later';

Share this post

Link to post
Share on other sites
Here is the form:

<form action="another_page.php" method="get">
    <input type="radio" name="gender" value="male" /> Male
    <input type="radio" name="gender" value="female" /> Female
    <input type="submit" name="submit" value="Go" />

When you click 'Go' on this form, it will take you to 'another_page.php'. On this page you need to make sure they arrived from that form by doing this:

if(isset($_GET['submit'])) {
    //I'll explain what goes here in a minute
else {
    //an error message here

Now, once we're in the if statement, we need to get the gender. (check the post before mine)

$gender = $_GET['gender'];
//whatever you want to do with $gender here

I have used GET instead of POST here. This way you can see the variables in the address bar in your browser. I like to use GETs until I have everything working right, then change everything to POST so the user doesn't see the variables.

Share this post

Link to post
Share on other sites


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.