简体   繁体   English

会话变量在使用后未更新

[英]Session variable not being updated after being used

I am trying to check the result from a function and determine where on my page it should go by using the Session Variable "alernativeRD". 我正在尝试检查函数的结果,并通过使用会话变量“ alernativeRD”来确定它应该在页面上的哪个位置。 It goes to the correct element on the first try, but after that it keeps going only to the first element regardless of whether its right or not. 第一次尝试时,它会转到正确的元素,但是此后,无论它是否正确,它只会继续访问第一个元素。 After some testing I've found that "alernativeRD" does get changed every time in the PHP function, but it doesn't change in the Javascript part. 经过一些测试,我发现PHP函数每次都确实更改了“ alernativeRD”,但在Javascript部分中却没有更改。

PHP PART PHP部件

function firstSignInDefault(){
    global $con;
    $clubUsername= $_SESSION['clubUsername'];
    $_SESSION['alternativeRD']='false'; //sets it back to false to avoid having alternativeRD be true    for next user

    $lastName= mysqli_real_escape_string($con, $_POST['lastNameF']);
    $firstName= mysqli_real_escape_string($con, $_POST['firstNameF']);
    $memberID= mysqli_real_escape_string($con, $_POST['idNumberF']);    

    if(!(is_numeric($memberID))){
        die("<h3> Student ID must be a number </h3>"); 
    }

    $getMemberRow= mysqli_query($con, "SELECT * FROM memberstable WHERE MemberMadeID='$memberID' AND Club='$clubUsername'");  

    if(mysqli_num_rows($getMemberRow)==0){                          
        $sql="INSERT INTO memberstable (MemberMadeID,FirstName,LastName,Club)    
        VALUES ('$memberID','$firstName','$lastName', '$clubUsername')";

        $test=false;              //checks to make sure sql statement runs fine
        if(mysqli_query($con,$sql))
            $test=true;
        else {
            echo "<h3> Error running sql </h3>";
        }

        $date=date("Y-m-d h:i:sa"); 

        $getMemberRow= mysqli_query($con, "SELECT * FROM memberstable WHERE MemberMadeID='$memberID' AND Club='$clubUsername'");  
        $memberRowArray=mysqli_fetch_array($getMemberRow);
        $memberPanID=$memberRowArray['UniquePanDBID'];

        $sql2="INSERT INTO signinstable (TimeOfSignIn, UniquePanDBID, ClubUsername, FirstName, LastName) VALUES ('$date','$memberPanID','$clubUsername', '$firstName', '$lastName')"; 
        //THE FOCUS OF THIS QUESTION IS BELOW THIS COMMENT
        if(mysqli_query($con, $sql2) && $test==true){
            $_SESSION['alternativeRD']='true'; 
            echo " <h2 id='signedInPeople' >".$date. " ".$firstName ." ". $lastName ."</h2>";
        }

    }
    else {
        echo "<h3> ID Number already in use</h3>";
    }
}

JAVASCRIPT/AJAX PART JAVASCRIPT / AJAX零件

function processFSIF(){

var xmlHttp= makeXMLHTTP();

// Create some variables we need to send to our PHP file
var url = "signInDataPlace.php";

var idNumberF = document.getElementById("idNumberF").value;
var lastNameF = document.getElementById("lastNameF").value;
var firstNameF = document.getElementById("firstNameF").value;
var typeSignIn="first"; 

var vars = "idNumberF="+idNumberF +"&lastNameF="+lastNameF +"&firstNameF="+firstNameF +"&typeSignIn=" +typeSignIn;

xmlHttp.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
xmlHttp.onreadystatechange = function() {
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        var return_data = xmlHttp.responseText;
        //AREA OF PROBLEM BELOW
        <?php
            if($_SESSION['alternativeRD']=='true'){              ///YOU ARE HERE, alternativeRD is acting stupid 
        ?>
            document.getElementById("serverInputList").innerHTML = return_data;
        <?php 
            }else{
        ?>
            document.getElementById("serverInputFSIF").innerHTML = return_data;
        <?php 
        }
        ?>

    }
}

// Send the data to PHP now... and wait for response to update the status div
xmlHttp.send(vars); // Actually executes the request
document.getElementById("serverInputFSIF").innerHTML = "processing...";

}

Your Javascript was printed only once, before you use AJAX. 在使用AJAX之前,您的Javascript仅被打印了一次。 You can return the session value together with response, or you can set the cookie in PHP, than use it in javascript. 您可以将会话值与响应一起返回,或者可以在PHP中设置cookie,而不是在javascript中使用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM