我的问题是:我为select标签
创建了php验证以下php脚本仅验证状态(选择标记) 从选择选项
中选择状态后,其未验证城市(选择标记)<?php
$state=$city="";
$stateErr=$cityErr="";
if ($_SERVER['REQUEST_METHOD']== "POST") {
$valid = true;
/*State Validation starts here*/
if(empty($_POST["parent_selection"]))
{
$stateErr="*State is Required";
$valid=false;
}
else
{
$state=test_input($_POST["parent_selection"]);
}
/*City Validation is Required*/
if(empty($_POST["child_selection"]))
{
$cityErr="* City is required";
$valid=false;
}
else
{
$city=test_input($_POST["child_selection"]);
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
}
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
var ArunachalPradesh = [
{display: "---Please Select---", value: "---Please Select---" },
{display: "Itanagar", value: "Itanagar" },
{display: "Arunachal Pradesh - Other", value: "Arunachal Pradesh - Other" },
];
var Assam = [
{display: "---Please Select---", value: "---Please Select---" },
{display: "Guwahati", value: "Guwahati" },
{display: "Silchar", value: "Silchar" },
{display: "Assam - Other", value: "Assam - Other" },
];
$("#parent_selection").change(function() {
var parent = $(this).val(); //get option value from parent
switch(parent){ //using switch compare selected option and populate child
case 'ArunachalPradesh':
list(ArunachalPradesh);
break;
case 'Assam':
list(Assam);
break;
default: //default child option is blank
$("#child_selection").html('');
break;
}
});
function list(array_list)
{
$("#child_selection").html(""); //reset child options
$(array_list).each(function (i) { //populate child options
$("#child_selection").append("<option value=\""+array_list[i].value+"\">"+array_list[i].display+"</option>");
});
}
});
</script>
<form action="select1.php" method="post">
State :<select name="parent_selection" id="parent_selection">
<option label="-- Please Select --"></option>
<option value="ArunachalPradesh">Arunachal Pradesh</option>
<option value="Assam">Assam</option>
</select><?php echo $stateErr?><br />
City : <select name="child_selection" id="child_selection">
</select><?php echo $cityErr?>
<input type="submit" value="submit"/>
</form>
答案 0 :(得分:0)
对于初学者来说,你设置的是$ valid = true;在您的城市错误案例
答案 1 :(得分:0)
if(empty($_POST["child_selection"]))
{
$cityErr="* City is required";
$valid=false;
}
这里需要一个(假)而不是真的,通过一个真实意味着你的城市,即使它是空的,它也会通过。
还尝试更改JS_script中的值并将其设置为空。
答案 2 :(得分:0)
除了@JMP提到的错误外,还有另一个错误:
根据你的javascript代码,你的空城市值是“---请选择---”而不是空,所以在php代码中,empty($_POST["child_selection"])
将始终为假。
修改您的javascript代码,将所有value: "---Please Select---"
更改为value: ""