我正在尝试使用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;
}
答案 0 :(得分:1)
对于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'];
}
?>
答案 1 :(得分:0)
如果要在检查用户是否选择一个值旁边验证值,可以使用以下方法:
$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代码没有任何问题。