0

我正在尝试使用 PHP 执行验证。我在使用 PHP 验证 Combobox 控件时遇到了一些问题。这是我的代码:

<select  >
<option value"gender">Gender</option> 
                <option value="male">Male </option>
                <option value="female">Female</option>
                <option value="other">other</option>
                </select>
I want validation in such a way that
If( Gender)
{
  Please select Gender;
}else
{
 Gender is selected;
}
4

2 回答 2

2

对于 HTML 端示例,

<form action="validation.php" method="GET">
 <select name="gender_select">
  <option value"gender">Gender</option> 
  <option value="male">Male </option>
  <option value="female">Female</option>
  <option value="other">other</option>
 </select>
</form>

对于 PHP 端示例(validation.php),

<?php

if($_GET['gender_select'] == "gender"){
  echo "Please select a gender";
}else{
  echo "You're ".$_GET['gender_select'];
}

?>
于 2013-10-13T17:06:59.337 回答
1

如果除了检查用户是否选择了值之外,还想验证值,可以使用以下方法:

$good_values = array('male', 'female', 'other');
if (false === in_array(strtolower($_POST['gender']), $good_values)) {
   echo 'Please select a valid gender.';
else
   echo 'You have selected ' . $_POST['gender'];

您的 HTML 代码没有任何问题。

于 2013-10-13T17:20:10.597 回答