Jump to content

Ajax call accurately sets $_SESSION variable, but page reload does not work


MockY

Recommended Posts

I've been trying to impliement something that is supposed to be easy and straighforward for some time now, but have not succeeded. I am somewhat close now and need a little help with the last step.

 

OBJECTION:

Clicking on a link (<a> tag) should execute a php code that sets a $_SESSION variable and displays the result to the user without reloading the page

 

WHAT WORKS:

I can click the link (I changed it to a layer instead, but the same rule applies) and it executes the desired php code and properly sets the $_SESSION varaible. However, I have to manually click the reload button on the browser in order for the browser to show the contents of the $_SESSION variable

 

I have tried to add various reloading methods (location.reload() for example) but that does not work. I'll simplify the code;

 

index.php

<?php  session_start();  ?>

<html>
<head>

<script>
function loadurl(dest) {

    try {
        xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        alert("Something went wrong here");
    }
        xmlhttp.onreadystatechange = triggered;
        xmlhttp.open("GET", dest);
        xmlhttp.send("null");
}

function triggered() {
    if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {

        document.getElementById("ajaxlink").innerHTML = xmlhttp.responseText;
    }
//    location.reload();

}
</script>


</head>
<body>


<div id="ajaxlink" onclick="loadurl('actions.php?action&q=1')">Click Here</div>


<?php echo $_SESSION['person']; ?>

</body>
</html>

actions.php

<?php
session_start();
 
if (isset($_GET['action'])) {
 
    $q = mysql_real_escape_string(trim($_GET["q"]));

    $query = "SELECT * FROM user WHERE id='$q' LIMIT 1";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    $_SESSION['person'] = $row['FirstName'] . ' ' . $row['LastName'];

 }
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.