简体   繁体   English

在第二个GET请求之后,第一页中的PHP会话变量的值未更新为第二页中的会话变量的值

[英]PHP session variable's value in 1st page not updating to value of session variable in 2nd page after 2nd GET request

I am using session variables in PHP. 我在PHP中使用会话变量。 I am making an ajax request from modifyDetails.php to another php file named getDetails.php using : 我正在使用以下命令从ModifyDetails.php向另一个名为getDetails.php的 php文件发出ajax请求:

xmlhttp.open("GET", "getDetails.php?val=" + str, true); xmlhttp.open(“ GET”,“ getDetails.php?val =” + str,true);

I am trying to change the values of form fields based on the selection made in dropdown list. 我正在尝试根据下拉列表中的选择更改表单字段的值。 I am getting it correct when I make a selection for the first time but when i make a different selection now , the values are still the same. 当我第一次进行选择时,我得到的是正确的,但是当我现在进行其他选择时,值仍然相同。 I have checked the values of session variable $_SESSION['Member_details'] in getDetails.php and it's values are perfectly fine but the session variable in modifyDetails.php is not updating it's value. 我已经检查了getDetails.php会话变量$ _SESSION [“Member_details”]的值,它的值是完全正常的,但在modifyDetails.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" “ 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'];

?> 

Ajax call happens after page load, you JS is getting rendered when the session is in State 'A'. Ajax调用在页面加载后发生,当会话处于状态“ A”时,将渲染您的JS。

Once you make your Ajax call, it`s in sate 'B', but at that point call to Session has already been made and the initial view is already rendered. 进行Ajax调用后,它的状态为“ B”,但此时已经对Session进行了调用,并且初始视图已呈现。

So, to fix the situation you should pass variables that are changing through Ajax reposne with JSON for example. 因此,要解决这种情况,您应该传递通过JSON通过Ajax reposne更改的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所选日期选择器值从第一页显示到第二页日期选择器 - How to display the selected datepicker value from the 1st page onto the 2nd page datepicker 在第一个json调用正常工作但第二个json调用不工作直到页面刷新 - After 1st json call working but 2nd json call not working till page get refresh 根据第一个下拉值清除第二个下拉值 - Clear 2nd Dropdown values based on 1st Dropdown value 在第一页上通过控制台浏览器触发按钮后,在第二页上操作数据 - Manipulating Data on 2nd page after button was triggered on 1st page via Console Browser 如何根据从第一个下拉菜单自动更改的第二个下拉菜单更改第二个文本框的值? - How to change the 2nd textbox value based on the 2nd dropdown that is automatically changed from 1st dropdown? 如果我在更改第一个控件的值时设置了第二个控件的值,则Ctrl + Z在第一个控件中不起作用 - Ctrl+Z not working in 1st control if I set 2nd control's value on change in 1st Control value 有没有办法使用第一个数组作为 Javascript 中的参考在第二个数组中获取相应的索引值? - Is there a way to get corresponding index value in a 2nd array using the 1st array as a reference in Javascript? 如何使用 componentDidMount() 发出多个 axios.get() 请求并将第一个响应值分配给第二个? - How to make multiple axios.get() requests with componentDidMount() and assign a response value of the 1st to the 2nd? 计算完成位置 1st、2nd=、2nd= 等 - Calculate finishing positions 1st, 2nd=, 2nd=, etc [javascript]使用来自第一个json的数据并将其转换为变量,然后使用该变量调用第二个json - [javascript]Use the data from 1st json and convert it to a variable and use that variable to call a 2nd json
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM