Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile ajax call refreshes page

    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:

    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!

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Well, sure it refreshes! You are *ASKING* it to!!!

    Code:
        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.
    Last edited by Old Pedant; 06-23-2010 at 12:33 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Macintosh

    Quote Originally Posted by Old Pedant View Post
    Well, sure it refreshes! You are *ASKING* it to!!!

    Code:
        window.location.href = xmlhttp.responseText;
    WHY would you do that???
    It seems to work. I'm open to more efficient solutions

    Quote Originally Posted by Old Pedant View Post
    What does your 5minforpage10.php look like???

    Show the code for it, maybe???
    PHP Code:
    <?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

    Quote Originally Posted by Old Pedant View Post
    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!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,635 Times in 4,597 Posts
    Okay...easy fix.

    Change this one line:
    Code:
    	window.location.href = xmlhttp.responseText;
    to something like this:
    Code:
          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:
    Code:
    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
    Code:
          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
    Code:
         location.href = ""; // blank returned from the PHP code
    and that in turn forced the refresh.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •