niko niko - 29 days ago 9
PHP Question

get the selected index value of <select> tag in php

I was trying to get the selected value from the

<select>
tag in PHP, but I get errors.

These is what I have done,

HTML

<select name="gender">
<option value="select"> Select </option>
<option value="male"> Male </option>
<option value="female"> Female </option>
</select>


PHP script

$Gender = $_POST["gender"];


but i get these error

Notice: Undefined index: gender in C:\xampp\htdocs\omnama\signup.php on line 7


php script

$Gender = isset($_POST["gender"]); ' it returns a empty string ? why ?


HTML

<form name="signup_form" action="./signup.php" onsubmit="return validateForm()" method="post">
<table>
<tr> <td> First Name </td><td> <input type="text" name="fname" size=10/></td></tr>
<tr> <td> Last Name </td><td> <input type="text" name="lname" size=10/></td></tr>
<tr> <td> Your Email </td><td> <input type="text" name="email" size=10/></td></tr>
<tr> <td> Re-type Email </td><td> <input type="text" name="remail"size=10/></td></tr>
<tr> <td> Password </td><td> <input type="password" name="paswod" size=10/> </td></tr>
<tr> <td> Gender </td><td> <select name="gender">
<option> Select </option>
<option value="male"> Male </option>
<option value="female"> Female </option></select></td></tr>
<tr> <td> <input type="submit" value="Sign up" id="signup"/> </td> </tr>
</table>
</form>


This is my php script

<?php
$con = mysql_connect("localhost","root","");
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$email = $_POST["email"];
$paswod = $_POST["paswod"];
$Gender = $_POST["gender"];
mysql_select_db("homepage");

if(mysql_num_rows(mysql_query("SELECT Email FROM users WHERE Email = '$email'",$con)))
{
echo "userid is already there";
}
else
{
$sql= "INSERT INTO users (FirstName, LastName,Email,Password,Gender)
VALUES
('$fname','$lname','$email','$paswod','$Gender')";

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "created";
}
?>


Please help me with these. I have to get the selected index value in the PHP.

I have read this link to use
<select>
tag in PHP.

Answer

Your form is valid. Only thing that comes to my mind is, after seeing your full html, is that you're passing your "default" value (which is not set!) instead of selecting something. Try as suggested by @Vina in the comment, i.e. giving it a selected option, or writing a default value

<select name="gender">
<option value="default">Select </option>    
<option value="male">   Male   </option>
<option value="female"> Female </option>
</select>

OR

<select name="gender">
<option value="male" selected="selected">   Male   </option>
<option value="female"> Female </option>
</select>

When you get your $_POST vars, check for them being set; you can assign a default value, or just an empty string in case they're not there.

Most important thing, AVOID SQL INJECTIONS:

//....
$fname   = isset($_POST["fname"]) ? mysql_real_escape_string($_POST['fname']) : '';
$lname   = isset($_POST['lname']) ? mysql_real_escape_string($_POST['lname']) : '';
$email   = isset($_POST['email']) ? mysql_real_escape_string($_POST['email']) : '';
you might also want to validate e-mail:
if($mail = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
  $email = mysql_real_escape_string($_POST['email']);
}
else
{
  //die ('invalid email address');
  // or whatever, a default value? $email = '';
}
$paswod  = isset($_POST["paswod"]) ? mysql_real_escape_string($_POST['paswod']) : '';
$gender  = isset($_POST['gender']) ? mysql_real_escape_string($_POST['gender']) : '';

$query = mysql_query("SELECT Email FROM users WHERE Email = '".$email."')";
if(mysql_num_rows($query)> 0)
{
  echo 'userid is already there';
}
else
{
 $sql = "INSERT INTO users (FirstName, LastName, Email, Password, Gender)
         VALUES ('".$fname."','".$lname."','".$email."','".paswod."','".$gender."')";
$res = mysql_query($sql) or die('Error:'.mysql_error());
echo 'created';
Comments