Jump to content

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 :]
Link to comment
https://forums.phpfreaks.com/topic/3732-radio-button/
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
if(!$result)
{
  echo 'There was a problem inserting your data into the databse please try again later';
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/3732-radio-button/#findComment-13079
Share on other sites

Here is the form:

[code]
<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>
[/code]

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:

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

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

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

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.
Link to comment
https://forums.phpfreaks.com/topic/3732-radio-button/#findComment-13082
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.