Jump to content

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


Go to solution Solved by 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'];

 }
?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.