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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7695599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:12:04  来源:igfitidea点击:

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

php

提问by niko

I was trying to get the selected value from the <select>tag in PHP, but I get errors.

我试图从<select>PHP 中的标记中获取选定的值,但出现错误。

These is what I have done,

这些是我所做的,

HTML

HTML

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

PHP script

PHP脚本

$Gender  = $_POST["gender"];

but i get these error

但我收到这些错误

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

php script

php脚本

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

HTML

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

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

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

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

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

回答by Damien Pirsy

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

您的表格有效。我唯一想到的是,在看到完整的 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>

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.

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

Most important thing, AVOID SQL INJECTIONS:

最重要的是,避免 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';

回答by niko

As you said..

如你所说..

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

because, you haven't mention method type either use POST or GET, by default it will use GET method. On the other side, you are trying to retrieve your value by using POST method, but in the form you haven't mentioned POST method. Which means miss-match method will result for empty.

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

Try this code..

试试这个代码..

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

and on signup page

并在注册页面上

$Gender  = $_POST["gender"];

i'm sure.. now, you will get the value..

我敢肯定.. 现在,您将获得价值..

回答by Kean Allen

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

it will echoes the selected value.

它将回显所选值。