0

update.php page

if (isset($_POST['bags']))
{
    $bagS=$_POST['bags'];
        $id=$_POST["id"];
        $_SESSION['id']=$id;
        $cats = explode(" ", $bagS);
        $cats = preg_split('/,/', $bagS, -1, PREG_SPLIT_NO_EMPTY);
        foreach($cats as $key => $cat )
        {
        $cat  = mysqli_real_escape_string($con,$cats[$key]);
        $cat = trim($cat);
 if($cat !=NULL)
             {
                 $stmt = $con->prepare('UPDATE wallet SET `Status`="Hold" where `Id`=? AND `bags`="'.$cat.'" ');
                 $stmt->bind_param("s", $_POST["id"]);
                 $stmt->execute();

  }
  }
}

want to use update.php file on index.php page on window.onbeforeunload

using ajax here

function myfoo(){

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

        window.onbeforeunload = function(){
      myfoo();
      return 'Are you sure you want to leave?';
    };
proofzy
  • 627
  • 1
  • 12
  • 23
  • what is problem with your code ? where your facing issue ? – JYoThI May 20 '17 at 10:21
  • i want to execute update query if browser close or click back button by user. iam using update.php and index.php file .so how to call update.php file on index.php when window.onbeforeunload fires –  May 20 '17 at 10:25
  • Possible duplicate of [JavaScript, browsers, window close - send an AJAX request or run a script on window closing](http://stackoverflow.com/questions/6162188/javascript-browsers-window-close-send-an-ajax-request-or-run-a-script-on-win) – proofzy May 20 '17 at 11:22

2 Answers2

1

1) your not sending any data like bags

2) ajax not defined type:'post' but your accessing value by post . if you not define the type means ajax will use default get method .

$.ajax({
            url: "update.php",
            type:'post',
            dataType: 'json',
            data: {id: 1,bags:bags}, // bags collection value what your goging to send to server 
            success: function (r) {}
});
JYoThI
  • 11,977
  • 1
  • 11
  • 26
  • iam also using $id=$_POST["id"]; so id: 1 means in Ajax? –  May 20 '17 at 10:37
  • 1
    id: 1 is equal to id=1 you can access it in php side like this $_POST['id'] ... data:{name:value,name1:value1....} – JYoThI May 20 '17 at 10:39
  • 1
    still not working. is right code of window.onbeforeunload.. function myfoo(){ $.ajax({ url: "update.php", type:'post', dataType: 'json', data: {id: 1,bags:bags}, // bags collection value what your goging to send to server success: function (r) {} }); } window.onbeforeunload = function(){ myfoo(); return 'Are you sure you want to leave?'; }; –  May 20 '17 at 10:43
  • 1
    is there any error in console tab ? did you included jquery ? @krishna – JYoThI May 20 '17 at 10:45
  • in using –  May 20 '17 at 10:54
  • and there is no warning or alert message when click close ..it directly closing the browser –  May 20 '17 at 10:56
0

working code just change one thing in it. Thanks @JYoThI https://stackoverflow.com/users/5933698/jyothi

$.ajax({
            url: "update.php",
            type:'post',
            dataType: 'json',
            data: {
                on_timeout: 1 // i just add this line
            },
 // bags collection value what your goging to send to server 
            success: function (r) {}
});
Community
  • 1
  • 1