会话变量在使用后未更新

时间:2014-11-13 21:34:26

标签: javascript php ajax

我正在尝试检查函数的结果,并通过使用会话变量" alernativeRD"来确定应该在我的页面上的位置。它在第一次尝试时转到正确的元素,但之后它继续只到第一个元素,无论它是否正确。经过一些测试后,我发现" alernativeRD"每次在PHP函数中都会更改,但它在Javascript部分中不会发生变化。

PHP PART

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

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...";

}

1 个答案:

答案 0 :(得分:0)

在使用AJAX之前,您的Javascript只打印过一次。您可以将会话值与响应一起返回,或者您可以在PHP中设置cookie,而不是在javascript中使用它。