【问题标题】:how to call update file in ajax on index.php page如何在 index.php 页面上调用 ajax 中的更新文件
【发布时间】:2017-05-20 10:07:05
【问题描述】:

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

【问题讨论】:

标签: php ajax onbeforeunload


【解决方案1】:

1) 您没有发送任何数据,例如 bags

2) ajax 未定义 type:'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) {}
});

【讨论】:

  • iam 也使用 $id=$_POST["id"];所以 id: 1 表示在 Ajax 中?
  • id: 1 等于 id=1 你可以像这样在 php 端访问它 $_POST['id'] ... data:{name:value,name1:value1.... }
  • 仍然无法正常工作。是 window.onbeforeunload 的正确代码.. function myfoo(){ $.ajax({ url: "update.php", type:'post', dataType: 'json', data: {id: 1,bags:bags} , // 袋子集合值你要发送到服务器的成功:function (r) {} }); } window.onbeforeunload = function(){ myfoo(); return '你确定要离开吗?'; };
  • 控制台选项卡中是否有任何错误?你包括 jquery 吗? @克里希纳
  • 在 中使用
【解决方案2】:

工作代码只需更改其中的一件事。谢谢@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) {}
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多