Jump to content


Photo

Radio Button


  • Please log in to reply
2 replies to this topic

#1 SieRobin

SieRobin
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts
  • LocationNew Jersey

Posted 28 February 2006 - 06:26 AM

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 :]

#2 php_b34st

php_b34st
  • Members
  • PipPipPip
  • Advanced Member
  • 168 posts

Posted 28 February 2006 - 06:45 PM

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.

//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
if(!$result)
{
  echo 'There was a problem inserting your data into the databse please try again later';
}


#3 soccer022483

soccer022483
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 28 February 2006 - 07:00 PM

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" />
</form>

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users