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 6 of 6
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    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".

  • #3
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    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

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,105
    Thanks
    23
    Thanked 594 Times in 593 Posts
    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.

  • Users who have thanked sunfighter for this post:

    shaunthomson (07-24-2012)

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,301
    Thanks
    10
    Thanked 586 Times in 567 Posts
    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 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • Users who have thanked rnd me for this post:

    shaunthomson (07-24-2012)

  • #6
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Cheers guys - I'm looking into the suggested ideas. Much appreciated.


  •  

    Posting Permissions

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