如何在 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) 你没有发送任何数据,比如 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) {}
});
工作代码只改变其中的一件事。谢谢@JYoThI https://whosebug.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) {}
});
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
在这里使用 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) 你没有发送任何数据,比如 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) {}
});
工作代码只改变其中的一件事。谢谢@JYoThI https://whosebug.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) {}
});