繁体   English   中英

如何在index.php页面上的ajax中调用更新文件

[英]how to call update file in ajax on index.php page

update.php页面

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();

  }
  }
}

想要在window.onbeforeunload index.php页面上使用update.php文件

在这里使用ajax

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?';
    };

1)您未发送任何数据,例如bag

2)ajax未定义类型:“ post”,但是您通过post访问的值。 如果未定义类型,则意味着ajax将使用默认的get方法。

$.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 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) {}
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM