0

There is a bit problem. I want to execute query whenever the browser or tab is closed, but if there exists any button or link in that page and i click on any of them then the query also execute. . Need help in this

First.php

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1
/jquery.min.js"></script>

<script type="text/javascript">

 $(document).bind('keypress', function(e) {
    if (e.keyCode == 116){
      validNavigation = true;
    }
  });

  // Attach the event click for all links in the page
  $("a").bind("click", function() {
    validNavigation = true;
  });

  // Attach the event submit for all forms in the page
  $("form").bind("submit", function() {
    validNavigation = true;
  });

  // Attach the event click for all inputs in the page
  $("input[type=submit]").bind("click", function() {
    validNavigation = true;
  }); 

 window.addEventListener("beforeunload", function (e) {
 if (!validNavigation) { 
 $.ajax({
url: 'logout.php',
type: 'POST',
async: false,
timeout: 4000
});
}
})
</script>

logout.php

    <?php
$username = "root";
$password = "";
$hostname = "localhost"; 

$dbhandle = mysql_connect($hostname, $username, $password) 
 or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";


$selected = mysql_select_db("examples",$dbhandle) 
  or die("Could not select examples");

$result =  mysql_query("DELETE FROM cars WHERE id='1'");
mysql_close($dbhandle);
?>
rahman_akon18
  • 336
  • 3
  • 14
  • 1
    And what exactly is your question? – Peter Clause Mar 08 '16 at 09:52
  • When user close the browser i want to execute the delete query . using onbeforeload function i can acheive it but the problem is the query execute when i press a button or click a link . I want to execute the query only when the browser is closed – rahman_akon18 Mar 08 '16 at 09:56
  • Possible (very likely) duplicate of http://stackoverflow.com/questions/1631959/how-to-capture-the-browser-window-close-event – Laurens Swart Mar 08 '16 at 09:59
  • The other answers and comments should help you fix the problem. And please change from `mysql_` to `mysqli_` functions, changeover is really straightforward and your code will be upgradable when you migrate to future PHP versions (as `mysql_` has been deprecated a while ago). – SaschaM78 Mar 08 '16 at 10:33

2 Answers2

0

The Event"beforeunload" is triggered when the browser unloads the current document. If you are using a form on the same page you need to unset the event when a submit is pressed.

for example like this: window.onbeforeunload=null;

you can do this in the onsubmit handler like this:

<form onsubmit="cancelUnloadAndSubmit()">
  Enter name: <input type="text">
  <input type="submit">
</form>
snitch182
  • 723
  • 11
  • 21
0

Wrap the click handlers like so:

<script type="text/javascript">
  var validNavigation = false;
  $(document).ready(function() {

    $("a").click(function() {
        validNavigation = true;
    });

  });

window.addEventListener("beforeunload", function (e) {
  if (!validNavigation) { 
    // do something...
  }
})
</script>
Peter Clause
  • 1,132
  • 9
  • 22