MockY Posted August 12, 2013 Share Posted August 12, 2013 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']; } ?> Link to comment https://forums.phpfreaks.com/topic/281078-ajax-call-accurately-sets-_session-variable-but-page-reload-does-not-work/ Share on other sites More sharing options...
MockY Posted August 12, 2013 Author Share Posted August 12, 2013 Once I put location.reload(); within the if statement in the triggered() function, it all went just fine. Link to comment https://forums.phpfreaks.com/topic/281078-ajax-call-accurately-sets-_session-variable-but-page-reload-does-not-work/#findComment-1444659 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.