2016-03-03 52 views
0

我在PHP中使用會話變量。我從做出一個Ajax請求modifyDetails.php名爲getDetails.php另一個PHP文件中使用:在第2頁GET請求後,第1頁中的PHP會話變量值不會更新爲第2頁中的會話變量值

xmlhttp.open( 「?getDetails.php VAL =」 「GET」,+ STR,真正的);

我想根據下拉列表中所做的選擇更改表單域的值。我在第一次做出選擇時得到了正確的結果 但是當我現在做出不同的選擇時,值仍然是相同的。 我檢查會話變量值$ _SESSION [「Member_details」]getDetails.php和它的值是完全正常的,但在 modifyDetails.php會話變量沒有更新它的價值。

「modifyDetails.php」

<?php 
    session_start(); 
    $fid = $_SESSION['fid_value']; 
    $get_Member_details = $_SESSION['Member_details']; 
?> 

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="ISO-8859-1"> 
<title>Modify Details</title> 
<script src="/prj/jquery.min.js"></script> 

<script> 
function fillData(str) 
{ 
    $(document).ready(function() 
    { 
     if (window.XMLHttpRequest){ 
      xmlhttp=new XMLHttpRequest(); 
     } 
     else{ 
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
     xmlhttp.onreadystatechange = function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     document.getElementById("kk").innerHTML = xmlhttp.responseText; 
    } 
    }; 
     xmlhttp.open("GET", "getDetails.php?val=" + str, true); 
     xmlhttp.send(); 

     //window.alert(str); 
     var jage= "<?php echo $get_Member_details['age'];?> "; 
     var jsex= "<?php echo $get_Member_details['sex'];?> "; 
     var jdob= "<?php echo $get_Member_details['dob'];?> "; 
     //alert(jdob); 
     var jrelation = "<?php echo $get_Member_details['Relationship'];?> "; 
     var jcontact = "<?php echo $get_Member_details['contact'];?> "; 
     var jaddress = "<?php echo $get_Member_details['address'];?> "; 
     var jpin = "<?php echo $get_Member_details['pin'];?> "; 

     document.getElementById("Age").value = jage; 
     //document.getElementById("Sex").value = jsex; 
     document.getElementById("Dob").value = jdob; 
     document.getElementById("Rel").value = jrelation; 
     document.getElementById("Contact").value = jcontact; 
     document.getElementById("Address").value = jaddress; 
     document.getElementById("Pin").value = jpin; 
    }); 
} 
</script> 
<style> 
    body{ 
    background-color:lightgrey; 
} 
#button { 
    background-color: green; 
    border: none; 
    color: white; 
    padding: 8px 14px; 
    text-align: center; 
    text-decoration: none; 
    font-size: 17px; 
    margin: 2px 4px; 
    cursor: pointer; 
} 
table { padding:2px 6px;} 
</style> 
</head> 
<body> 
    <h1>Customer Form </h1> 
    <h3>Please fill in the details below:</h3> 
     <form action="modified.php" method="post"> 
     <table style="width:24%;"> 
      <tr><td>Family ID:</td><td><input type="text" name="fid" value="<?php echo "$fid";?>" readonly /></td></tr><tr></tr> 
      <tr><td>Name:</td> 
      <td> 
       <?php 
       mysql_connect('localhost', 'root', ''); 
       mysql_select_db('project'); 

       $sql_query = "SELECT name FROM family WHERE fid=$fid"; 
       $records = mysql_query($sql_query); 

       echo"<select name='member_name' onchange='fillData(this.value)'>";   
       while($family_member = mysql_fetch_array($records)) 
       { 
        echo "<option value='".$family_member['name']."' >".$family_member['name']."</option>";       
       } 
       echo "</select></td></tr>"; 

       ?> 
     <div id="kk"> 
     </div> 
      <tr><td>Age:</td><td><input id="Age" type="text" name="age" required /></td></tr><tr></tr> 
      <tr><td>Sex:</td><td><input id="Sex" type="radio" name="sex" value="Male" required />Male 
           <input id="Sex" type="radio" name="sex" value="Female" required/>Female</td></tr><tr></tr> 
      <tr><td>Date of Birth:</td><td><input id="Dob" type="text" name="dob" required /></td></tr><tr></tr> 
      <tr><td>Relationship:</td><td><input id="Rel" type="text" name="relation" list="relations" required> 
         <datalist id="relations"> 
           <option value="Son">Son</option> 
           <option value="Daughter">Daughter</option> 
           <option value="Father">Father</option> 
           <option value="Mother">Mother</option> 
           <option value="GrandMother">GrandMother</option> 
           <option value="GrandFather">GrandFather</option></td></tr><tr></tr> 
         </datalist></td></tr> 
      <tr><td>Contact Number:</td><td><input id="Contact" type="text" name="contact" required /></td></tr><tr></tr> 
      <tr><td>Address:</td><td><input id="Address" type="text" name="address" required /></td></tr><tr></tr> 
      <tr><td>Pincode:</td><td><input id="Pin" type="text" name="pin" required /></td></tr><tr></tr> 
     </table> <br> 

     <input id="button" type="submit" value="Submit"/> 
     </form> 

     <br><br><br> 
     <a href="search.html"> <h4>Back to Search Page</h4></a> 

</body> 

「getDetails.php」

<?php 
    session_start(); 
    $fid =$_SESSION['fid_value']; 
?> 

<?php 
$name=$_GET['val']; 
echo $name; 
mysql_connect('localhost', 'root', ''); 
mysql_select_db('project'); 

$sql_query = "SELECT * FROM family WHERE fid=$fid AND name='$name'"; 
$records = mysql_query($sql_query); 

$_SESSION['Member_details'] = mysql_fetch_array($records); 

$hold = $_SESSION['Member_details']; 

//echo $hold['dob']; 
//echo $hold['contact']; 

?> 

回答

1
頁面加載後

Ajax調用發生時,你JS是越來越呈現時會議在國家'A'。

一旦你進行了Ajax調用,它就處於'B'狀態,但是在那個時候調用Session已經完成,並且初始化視圖已經被渲染。

因此,要解決這種情況,您應該通過例如通過Ajax reposne傳遞更改的變量。