在php </select>中获取<select>标签的选定索引值

时间:2011-10-08 08:11:23

标签: php

我试图从PHP中的<select>标记中获取所选值,但是我收到错误。

这就是我所做的,

HTML

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

PHP脚本

$Gender  = $_POST["gender"];

但是我收到了这些错误

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

php脚本

$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>

这是我的php脚本

  <?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";
  }
 ?> 

请帮我解决这些问题。我必须在PHP中获取所选的索引值。

我已阅读此link以在PHP中使用<select>标记。

3 个答案:

答案 0 :(得分:6)

您的表格有效。在我看到你的完整HTML之后,我想到的只是你传递了你的“默认”值(没有设置!)而不是选择一些东西。 按照@Vina在评论中的建议尝试,即给它一个选定的选项,或者写一个默认值

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

当你获得$ _POST vars时,检查它们是否被设置;你可以分配一个默认值,或者只是一个空字符串,以防它们不存在。

最重要的是,避免SQL注入:

//....
$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';

答案 1 :(得分:2)

正如你所说..

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

因为,您没有提到方法类型使用POST或GET,默认情况下它将使用GET方法。另一方面,您尝试使用POST方法检索您的值,但在表单中您没有提到POST方法。这意味着miss-match方法将导致为空。

试试这段代码..

<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 value="select">                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>

并在注册页面上

$Gender  = $_POST["gender"];

我确定..现在,你将获得价值..

答案 2 :(得分:-1)

$gender = $_POST['gender'];
echo $gender;  

它会回显所选的值。