2014-02-14 52 views
1

問題:取決於選擇標記得到清除後提交

如果我選擇從選擇選項只有狀態然後我點擊沒有選擇城市提交按鈕,然後選擇城市(選擇標籤)驗證發生,但爲什麼城市選擇標籤選項是越來越清

幫助我的朋友在這裏我犯了一個錯誤:

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

} 

elseif(empty($_POST["child_selection"])) 
    { 
     $cityErr="* City is required"; 
     $valid=false; 
    } 


    else 
    { 
    $state=test_input($_POST["parent_selection"]); 
    $city=test_input($_POST["child_selection"]); 
    } 


function test_input($data) 
    { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
    } 
} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<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: "" }, 
    {display: "Itanagar", value: "Itanagar" }, 
    {display: "Arunachal Pradesh - Other", value: "Arunachal Pradesh - Other" }, 
    ]; 

var Assam = [ 
    {display: "---Please Select---", value: "" }, 
    {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> 

<title>Untitled Document</title> 
</head> 

<body> 
<form action="select1.php" method="post"> 

State :<select name="parent_selection" id="parent_selection"> 
<option label="-- Please Select --"></option> 
    <option value="ArunachalPradesh" <?php if(isset($_POST["parent_selection"]) && $_POST["parent_selection"] == "ArunachalPradesh") { ?> selected="selected" <?php } ?>>ArunachalPradesh</option> 
    <option value="Assam" <?php if(isset($_POST["parent_selection"]) && $_POST["parent_selection"] == "Assam") { ?> 
     selected="selected" <?php } ?>>Assam</option> 
    </select><?php echo $stateErr?><br /> 
City : <select name="child_selection" id="child_selection"> 
</select><?php echo $cityErr?><br /> 
<input type="submit" value="submit"/> 

</form> 
</body> 
</html> 
+0

幫助我的朋友在這裏我犯了一個錯誤 – user3309116

回答

1

你的JavaScript更改此

<script language="javascript" type="text/javascript"> 
    $(document).ready(function(){ 
     var ArunachalPradesh = [ 
     {display: "---Please Select---", value: "" }, 
     {display: "Itanagar", value: "Itanagar" }, 
     {display: "Arunachal Pradesh - Other", value: "Arunachal Pradesh - Other" }, 
     ]; 

    var Assam = [ 
     {display: "---Please Select---", value: "" }, 
     {display: "Guwahati", value: "Guwahati" }, 
     {display: "Silchar", value: "Silchar" }, 
     {display: "Assam - Other", value: "Assam - Other" }, 
     ]; 

    jQuery(window).on("load", function(){ 

     $('#parent_selection').change(); 

    }); 

     $("#parent_selection").on('change',function() { 
      // var parent = $(this).val(); //get option value from parent 
      var parent = document.getElementById("parent_selection").value; 
      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> 
+0

它在$ _ POST [「parent_selection」]不工作 – user3309116

+0

你得到的價值? – Dev

+0

是的,城市選擇標記(child_selection)獲取清除後提交沒有選擇從形式的城市 – user3309116