是什么导致我的ajax无法执行所有命令? [英] what would cause my ajax to not follow through all commands?
问题描述
我的插入Ajax代码将刷新页面,但不会发出警报(添加成功").数据确实回显了我仔细检查过的右键触发字ADD_OK.我没有错误.这对我来说似乎很奇怪,因为我在添加和删除操作中使用了完全相同的ajax代码.
My insert ajax code will refresh the page but then not alert ("add successful"). Data does echo the right key trigger words ADD_OK i double checked. I have no errors. This seems odd to me because I used the exact same ajax code for both my add and delete.
代码不会发出警报,但会添加到数据库中
<script type="text/javascript">
$(document).on('click','#insert_btn',function (){
$.ajax({
type:'POST',
url:'add.php',
datatype: "json",
data: {
first_name: $("#first_name").val(),
last_name: $("#last_name").val(),
position: $("#position").val(),
date: $("#date").val(),
updated: $("#updated").val(),
},
success: function(data){
if (data=='ADD_OK') {
// alert(data);
location.reload();
alert("Add Successful");
} else {
//alert('something wrong');
}
}
})
});
</script>
代码同时执行删除和警报相同的删除代码将刷新页面并发出警报(删除成功").是什么原因造成的?
Code Working doing both the delete and alert The same code for delete will refresh page and alert ("delete successful"). What would cause this?
<script type="text/javascript">
$(document).on('click','.delete_btn',function (){
if (confirm('Are you sure you want to delete this?')) {
var id = $(this).attr("id").match(/\d+/)[0];
var del_id = $('#example').DataTable().row( id ).data();
var del_id = del_id[0];
$.ajax({
type:'POST',
url:'delete.php',
data: {del_id:del_id},
success: function(data){
if (data=='DEL_OK') {
location.reload();
alert("Delete Successful!");
} else {
// alert('something wrong');
}
}
});
}
});
</script>
Add.php代码
<?php
$first_name = strtoupper($_POST['first_name']);
$last_name = strtoupper($_POST['last_name']);
$position = strtoupper($_POST['position']);
$date = $_POST['date'];
$updated = $_POST['updated'];
$stmt = $conn->prepare("INSERT INTO employees (first_name, last_name, position, date, updated) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssss', $first_name, $last_name, $position, $date, $updated);
$add = $stmt->execute();
//print_r($_POST);
if($add) {
echo "ADD_OK";
}
?>
推荐答案
交换您的 location.reload()
和 alert(...)
行.
仅在页面尚未开始重新加载时,警报才会运行.我想出于某些原因,在某些页面上花费的时间会更长.
The alert will only run if the page hasn't started reloading yet. I imagine it takes longer on some pages for a number of reasons.
这篇关于是什么导致我的ajax无法执行所有命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!