0

That is my code

function myfoo(){
    $.ajax({
            type: "POST",
            url: "update.php",
            dataType: 'json',
            data: {
              on_timeout: 1
            },
            success: function (r) {}
       });
    }

     window.onbeforeunload = function(){
          myfoo();
          return 'Are you sure you want to leave?';
     };

my update.php code:

session_start(); 
include 'conn.php';

if(isset($_SESSION['Seats'])) {
    $seatS=$_SESSION['Seats']; 
    $Eventid=$_SESSION['Eventid'];
    $cats = explode(" ", $seatS); 
    $cats = preg_split('/,/', $seatS, -1, PREG_SPLIT_NO_EMPTY); 
    foreach($cats as $key => $cat ){ 
        $cat = mysql_real_escape_string($cats[$key]); 
        $cat = trim($cat);
        if($cat !=NULL) { 
            $stmt = $con->prepare('UPDATE fistevent SET Status=" " where Event_Id=? AND seats="'.$cat.'" AND Status="Hold" '); 
            $stmt->bind_param("s", $_SESSION['Eventid']); 
            $stmt->execute(); 
        }
    }
}
Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26

1 Answers1

0

Try below beforeunload binding:

$(window).on('beforeunload', function () {
   return 'Are you sure you want to leave?';
});
$(window).on('unload', function () {
   console.log('calling ajax');
   myfoo();
});
Mahesh Singh Chouhan
  • 2,558
  • 1
  • 16
  • 26
  • not working sir.now It Does not show any alert message.means leave or stay –  May 22 '17 at 07:35
  • are you getting any confirm box? with ok and cancel option? It will execute function only when you click on ok button – Mahesh Singh Chouhan May 22 '17 at 07:36
  • my above code is running condition.. and it is also showing leave or stay alert. but when iam click on close browser it execute the query direct.but i want to execute query when click onto leave button –  May 22 '17 at 07:39
  • @krishna as i understood when user clicks on close tab of the browser the alert box must be executed. i'm i right – Pavan Baddi May 22 '17 at 08:10
  • sir i know above code is perfect.but please try my code on your localhost.what is the problem in it –  May 22 '17 at 08:11
  • @PavanBaddi the code showing alert message .but when i click on close button it directly execute the query. –  May 22 '17 at 08:15
  • @PavanBaddi i want to execute query when click onto leave button –  May 22 '17 at 08:16
  • @MaheshSinghChouhan sir now showing alert message but not updating the records on confirm to leave –  May 22 '17 at 08:25
  • @krishna it should work, ok try reloading the page and check when you click on reload button then ajax is loading? check browser console, it will console log message before calling ajax – Mahesh Singh Chouhan May 22 '17 at 08:27
  • my update.php code......session_start(); include 'conn.php';if(isset($_SESSION['Seats'])) {$seatS=$_SESSION['Seats']; $Eventid=$_SESSION['Eventid'];$cats = explode(" ", $seatS); $cats = preg_split('/,/', $seatS, -1, PREG_SPLIT_NO_EMPTY); foreach($cats as $key => $cat ){ $cat = mysql_real_escape_string($cats[$key]); $cat = trim($cat);if($cat !=NULL) { $stmt = $con->prepare('UPDATE fistevent SET `Status`=" " where `Event_Id`=? AND `seats`="'.$cat.'" AND `Status`="Hold" '); $stmt->bind_param("s", $_SESSION['Eventid']); $stmt->execute(); }}} –  May 22 '17 at 08:27
  • @MaheshSinghChouhan sir. can you tell me please why firefox not supporting this means unload and beforeunload ?on firefox it shows nothing even any alert, but on chrome it is working –  May 22 '17 at 09:38
  • @krishna change `beforeunload` to `onbeforeunload` and check in firefox – Mahesh Singh Chouhan May 22 '17 at 09:40
  • check [this](http://stackoverflow.com/questions/20773306/mozilla-firefox-not-working-with-window-onbeforeunload#35484886) answer for firefox issue, and run code snippet in firefox, he is saying that user should click first on document – Mahesh Singh Chouhan May 22 '17 at 09:43
  • @MaheshSinghChouhan sir .don't understand what they are trying to do. what i need to do in my code? –  May 22 '17 at 09:54
  • @MaheshSinghChouhan yes sir it is working and showing leave stay message but did not updating records like in chrome –  May 22 '17 at 10:03