Janus
Janus

Reputation: 11

Radio Button Values, Send ONLY one form parameter

I want to use radio buttons in my application.When i click Male , i want it to input male into the database, when i click Female, it sends female into the database.

Code looks like this

<html>
<head></head>
<title>Radio Test</title>
<body>
<h1>Radio Test</h1>
<form name="testform" id="testform" method="post" action="test.php" />
Name : <input type="text" name="fullname" id="fullname" /></br>
    Male <input type="radio" name="male" id="male" value="Male" />
    Female <input type="radio" name="female" id="female" value="FeMale" /></br></p>
        <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

For the HTML Area, Now i want to make it output values, but it doesnt respond. My PHP looks like this but instead of sending one value, it sends both

<?php

$host = "127.0.0.1"
$user = "root"
$pass = ""
$db = "people_info"

$con = mysqli_connect($host, $user, $pass, $db) or die('Cannot Connect :'.mysqli_error());
$fullname = mysqli_real_escape_string($con, $_POST['fullname']);
$male = mysqli_real_escape_string($con, $_POST['male']);
$female = mysqli_real_escape_string($con, $_POST['female']);

$sql = "insert into data (fullname,male,female) values ('".$fullname."', '".$male."', '".$female."')";
mysqli_query($con,$sql) or die ('Failed Query :'.mysqli_error($con));
mysqli_close($con);
?>

I was thinking of using switch case, but that made it even worse. It didnt send any values as well..

Upvotes: 0

Views: 210

Answers (2)

Amruth Pillai
Amruth Pillai

Reputation: 1693

The name attribute in a radio input specifies it's radio group. Therefore, when you try to submit the form, it sends both values. Specify the name attribute of both to something like gender, and it would work.

<label for="male">Male</label> 
<input type="radio" name="gender" id="male" value="male" />

<label for="female">Female</label> 
<input type="radio" name="gender" id="female" value="female" />

And in the PHP portion, you just need to request that value once, so do it with:
$gender = mysqli_real_escape_string($con, $_POST['gender']);

Upvotes: 0

Ravi
Ravi

Reputation: 31417

I think, you should have one column Gender in your table. I'm not sure why are you storing it in two different column.

Change your code to following

Male <input type="radio" name="gender" id="male" value="Male" />
Female <input type="radio" name="gender" id="female" value="FeMale" /></br></p>

Then,

 $gender = mysqli_real_escape_string($con, $_POST['gender']);

Upvotes: 1

Related Questions