Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-17-2012, 04:48 AM   PM User | #1
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
changing what AJAX does on return from php?

Gidday

I'm using AJAX to run a simple php script that updates a database. On success, php echoes a success message, which gets displayed in a javascript alert.

I also have a totally separate timeout script running on all pages, to check for inactivity of more than 20mins. If there is a timeout, the user gets redirected to the login page like so:

header('Location: http://www.mysite.com/login');

The timeout works fine, except when using ajax as above, the whole login page html gets displayed in the alert.

It makes sense to me why it does it, but I'm unsure of the correct way to get around the problem. Is there a way to override the AJAX process in the event of a timeout, and redirect to the login page?

Thanks for your time and help.
shaunthomson is offline   Reply With Quote
Old 07-17-2012, 01:22 PM   PM User | #2
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 952
Thanks: 7
Thanked 98 Times in 98 Posts
WolfShade is an unknown quantity at this point
Can't say without seeing your code. (Please use the CODE tags.)
__________________
^_^

If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
*
The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
WolfShade is offline   Reply With Quote
Old 07-17-2012, 02:01 PM   PM User | #3
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
The AJAX is:

Code:
	
$('.w').live("click",function() {	
	
  var id2 = $(this).attr("id");
  var dataVar2 = {s:id2 };		
	
  $.ajax({
			
    type: "POST",
    url: "<?php $_SERVER['DOCUMENT_ROOT'] ?>/save.php",			
    data: dataVar2,			
    beforeSend:  function() {
	<!---->  
    },
				
    success: function(data){
    
    alert(data);
    },
    error: function(data){
    alert('Site error 1: Connection failure.  If the problem persists, please contact support');
  }
  });				
		
 return false;	
});


The save.php is:

PHP Code:
if (isset($_SESSION['uname'])) {

    if (isset(
$_POST['mydata'])) {
        
        
$ID =     $_POST['mydata'];    
        
        if (
is_numeric ($ID)) {        
        
            
save($ID);        
        }
        else {
            
            echo 
'The data sent was suspicious and did not pass validation';
        }    
    }
    else {
        
        
header('Location: http://www.mysite.com');
    }    
}
else {
    
    echo 
'You must be logged in to do that.';    
    
}

function 
save($ID){    

    
$ID mysql_real_escape_string($ID);    
    
    
$query  "INSERT INTO `mycol`(`u_id`) VALUES ('{$ID}')";
    
    if (!(
$result mysql_query($query))) {echo 'Site error 2: Connection failure.  If the problem persists, please contact support';} else {
        
        echo 
'The data was added';
    }



The php timeout script, which redirects to a standard login form page:

PHP Code:
if (isset($_SESSION['u_name'])) {
        
    if (isset(
$_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) {//if last request was more than 30 minutes ago, expire...
    
        
$error_message 'Your session timed out as your last activity was over 30 mins ago.  Please login again:';
        
forceLogin($error_message);
        exit();
    }
    
    
$_SESSION['LAST_ACTIVITY'] = time();
}



function 
forceLogin($error_message) {    
    
    
$_SESSION = array();
    
session_destroy();
    
    
session_start();
    
session_regenerate_id();
        
    
$_SESSION['LOGIN_ERROR_MESSAGE'] = $error_message;    

    
    
header('Location: http://www.mysite.com/login');    


Cheers
shaunthomson is offline   Reply With Quote
Old 07-17-2012, 10:24 PM   PM User | #4
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,392
Thanks: 18
Thanked 351 Times in 350 Posts
sunfighter is on a distinguished road
Why not have a universal lockout var set to false. When your click function starts ajax set the lockout to true. When the alert is shown reset lockout to false.

In your timer have it check the lockout var just before the relocation, abort and reset the timer if it is true.
sunfighter is offline   Reply With Quote
Users who have thanked sunfighter for this post:
shaunthomson (07-24-2012)
Old 07-18-2012, 06:13 AM   PM User | #5
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
look for the html of the page and do the right thing if it shows up:

Code:
 success: function(data){
    if(  String(data).match(/<head>/i) ){ 
     return location.href="http://www.mysite.com/login"; 
  }   


    alert(data);
    },
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
shaunthomson (07-24-2012)
Old 07-24-2012, 05:55 PM   PM User | #6
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
Cheers guys - I'm looking into the suggested ideas. Much appreciated.
shaunthomson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:20 PM.


Advertisement
Log in to turn off these ads.