Send query to database while closing browser
On my website name combiner, I want to send query to database while closing browser i have tried this script but it didn't execute the query in database to change the value of column statue
$(window).on("beforeunload", function() {
<?php
include('config/configuration.php');
$uid=$_GET['id_p'];
$statue_pharmacie = mysql_query('update pharmacies set statue=0 where id="'.$uid.'"');
?>
return inFormOrLink ? "Do you really want to close?" : null;
});
Solution 1:
Here's a basic example (assumes you're using jQuery), but I would be cautious as unload events typically have varied browser support:
Javascript:
<script>
// Grab ID for use later
var data = <?php (isset($_GET['id_p']) ? (int) $_GET['id_p'] : 0); ?>;
// On page unload
$( window ).unload(function() {
// Send data to script
$.post( "ajax.php", function( data ) {
// Handle return
});
});
</script>
ajax.php
<?php
// Make sure 0 wasn't passed
if (!empty($_POST['data'])) {
// Run query
}
You can read about AJAX here: http://api.jquery.com/jQuery.ajax/ and the shorthand POST here: http://api.jquery.com/jQuery.post/
Also, I would stay away from the MySQL library of PHP as it's deprecated. You should be using PDO: http://php.net/manual/en/book.pdo.php
Thanks,
Andrew