我有一个触发按钮功能的按钮
<input type="button" value="Change Stuff" onClick="Change()" />
这是上述功能:
function Change()
{
var z = confirm("You are about to change something!");
if (z){
var value1 = prompt("Change Value1 : ");
var value2 = prompt("Change Value2 : ");
var value3 = prompt("Change Value3 : ");
$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
id: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script'
});
} else {
return false;
}
}
AJAX调用的数据
<?php
include "connect.php";
$id = $_POST["id"];
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$value3 = $_POST["value3"];
$currentDate = date("Y-m-d");
$sql = $conn->query("UPDATE tbl_table1 SET datum='". $currentDate ."',value1='". $value1 ."', value2='". $value2 ."',value3='". $value3 ."',einstellung='". 2 ."' WHERE id =" . $id) or die('Error Updating table_1 - Error Message : ' .mysqli_error($conn));
if ($conn->affected_rows == 0){
echo '<script type="text/javascript"> alert(\'No data found!\'); </script>';
} else {
echo '<script type="text/javascript"> alert(\'Success!\'); </script>';
$result = $conn->query("UPDATE tbl_table2 SET status='" . 1 ."' WHERE id =" . $id) or die('Status could not be set - Error Message : ' .mysqli_error($conn));
}
mysqli_free_result($sql);
mysqli_free_result($result);
?>
不确定是否重要但也在我的代码中(重新加载以查看勾选的复选框):
$(document).ajaxStop(function(){
location.reload(true);
});
我唯一的问题是:警报永远不会出现。 我尝试在测试页面上重建它 给定变量值而不是$ _POST,并在我直接调用网站时发出警报 我对AJAX了解不多,这实际上是我第一次使用它。所以我的问题是 是,是AJAX阻止alert()还是我的代码中有错误?
这背后的主要思想是:检查id是否在表中,是否更新了某些值,然后通知找到了匹配项。然后更新table2。否则只是警告id不在表中。
答案 0 :(得分:0)
你应该使用ajax请求的回调函数。之后,根据回复,在回调中,你应该调用警报功能
答案 1 :(得分:0)
如果您重新加载将页面重置为默认值的页面。你应该使用
像这样的ajax设置:$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
uid: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script',
success: function(response){
alert(response);
}
});
答案 2 :(得分:0)
试试这个
function Change()
{
var z = confirm("You are about to change something!");
if (z){
var value1 = prompt("Change Value1 : ");
var value2 = prompt("Change Value2 : ");
var value3 = prompt("Change Value3 : ");
$.ajax({
url: ('ajax-call.php'),
type: 'POST',
data: {
id: '<?php echo $_GET["id"]; ?>',
value1: value1,
value2: value2,
value3: value3
},
dataType: 'script'
}).done(function (msg) {
alert( msg );
}
document.location = "cableList.php";
});;
} else {
return false;
}
}
PHP
<?php
include "connect.php";
$id = $_POST["id"];
$value1 = $_POST["value1"];
$value2 = $_POST["value2"];
$value3 = $_POST["value3"];
$currentDate = date("Y-m-d");
$sql = $conn->query("UPDATE tbl_table1 SET datum='". $currentDate ."',value1='". $value1 ."', value2='". $value2 ."',value3='". $value3 ."',einstellung='". 2 ."' WHERE id =" . $id) or die('Error Updating table_1 - Error Message : ' .mysqli_error($conn));
if ($conn->affected_rows == 0){
echo 'No data found!';
} else {
echo 'Success';
$result = $conn->query("UPDATE tbl_table2 SET status='" . 1 ."' WHERE id =" . $id) or die('Status couldn't be set - Error Message : ' .mysqli_error($conn));
}
mysqli_free_result($sql);
mysqli_free_result($result);
?>