...

View Full Version : ajax call refreshes page



levivel
06-22-2010, 11:17 PM
I'm poking in the blind with javascript and hope that you can help with this issue.

A javascript function is making a call to an external php file, which checks the compares the current time with a time stored in a mysql database. If a certain time passes, the php will send back instructions to redirect the user to a different page.

The issue is that the page refreshes every time the is javascript function is executed. This resets any forms, or videos that the user is watching on the page.

Here is the relevant code:


function checkTimer()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
window.location.href = xmlhttp.responseText;
}
}
xmlhttp.open("POST","php/5minforpage10.php",true);
xmlhttp.send();
}

window.setInterval(checkTimer,15000);

Is there a way to achieve the goal of checking the time against the server time (via the php code) without refreshing the page?

Thanks kindly!

Old Pedant
06-22-2010, 11:31 PM
Well, sure it refreshes! You are *ASKING* it to!!!



window.location.href = xmlhttp.responseText;

WHY would you do that???

Normally, you would use the responseText to update some part of the existing page. Maybe change the innerHTML of some <div> or maybe use JSON to make more than one change. Maybe...

What does your 5minforpage10.php look like???

Show the code for it, maybe???

Is there some reason you can't just set a timer when this page first loads and have it run off to the other page when the timer expires? That would presumably work if nothing is changing that DB field you are checking while this page is running. Naturally, if the DB contents are completely dynamic, then you want to use something like this...but *NOT* this.

levivel
06-23-2010, 12:07 AM
Well, sure it refreshes! You are *ASKING* it to!!!



window.location.href = xmlhttp.responseText;

WHY would you do that???
It seems to work. I'm open to more efficient solutions



What does your 5minforpage10.php look like???

Show the code for it, maybe???


<?php
$conn = mysql_connect($server, $username, $password) or die (mysql_error());
mysql_select_db($database,$conn) or die(mysql_error());
$query = mysql_query("SELECT DATE_ADD(spit_timer_start,INTERVAL '5' MINUTE) as expiry FROM timer WHERE collins_code=$collins_code AND ispr_code=$ispr_code") or die(mysql_error());

$row = mysql_fetch_array($query);
$checkTime = $row['expiry'];
$currentTime = date('Y-m-d H:i:s');

if ($currentTime > $checkTime){
$redirect_url = "page10.php";
echo $redirect_url;
}
?>

I welcome "why would i do that" comments on the php code too, especially if you point me to a better solution :)



Is there some reason you can't just set a timer when this page first loads and have it run off to the other page when the timer expires? That would presumably work if nothing is changing that DB field you are checking while this page is running. Naturally, if the DB contents are completely dynamic, then you want to use something like this...but *NOT* this.

Various content will be displayed to the user across multiple pages; such as a multi-page form.

Do you have any suggestions how I could check on this timer and initiate a redirect without having to refresh the page? Thanks!

Old Pedant
06-23-2010, 12:28 AM
Okay...easy fix.

Change this one line:


window.location.href = xmlhttp.responseText;

to something like this:


var url = xmlhttp.responseText;
if ( url.replace(/\s/g,"") != "" ) window.location.href = url;

See, your PHP code is fine: It only returns a the URL ("page10.php") when the timer says it should.

The rest of the time it presumably returns nothing. A blank string.

So just make sure the AJAX code does nothing when the PHP code returns nothing!

If you wanted to be really safe, you could maybe change the PHP code to something like:


if ($currentTime > $checkTime){
echo "page10.php";
} else {
echo "NO";
}
exit(); // or is it just exit; ??? Not a PHP user
?>

The exit will ensure that no junk gets added on to the end of the echo.

The echo of "NO" makes for something safe to test for in the AJAX code. So now you'd change it to do


var url = xmlhttp.responseText;
if ( url != "NO" ) window.location.href = url;


So nothing fundamental wrong with what you were doing except that you were doing the location.href regardless.

I think the reason it refreshed your page is because you were, essentially, doing

location.href = ""; // blank returned from the PHP code

and that in turn forced the refresh.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum