如何使用 $.ajax 将变量传递给 alert()?
How can I pass a variable to alert() with $.ajax?
我想让用户点击按钮确认删除记录,然后按钮将值 Yes 发送到 $_POST 请求,这样我就可以使用 PHP 删除记录,而不是它向我展示了包含函数的 javascript 文件。
我哪里做错了,我一直在修补和搜索 3 个小时,而在我的脑海里,我知道这很容易解决。
非常感谢任何帮助。
如果您认为有更聪明的方法来执行此操作,请告诉我。
$(function () {
$("#DeleteConfirm").dialog({
buttons: {
"Confirm": function() {
var DeleteConfirmation = "Yes";
$.ajax({
type: "POST",
url: "index.php",
data: DeleteConfirmation,
success: function(DeleteConfirmation){
alert(DeleteConfirmation);
},
dataType: "text"
});
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
});
您需要将 YES
值分配给 属性 名称:
data: { "confirmed": DeleteConfirmation},
然后在您的 PHP 中,您可以使用 $_POST["confirmed"]
准备好它。
然而,这似乎有点多余,因为用户已经单击按钮以确认删除,因此在 POST 请求中发送值似乎没有什么价值。发送要删除的项目的 id
会更有意义。
您的 ajax 格式错误,请尝试:
$.ajax({
type: "POST",
url: "index.php",
data: {answer: DeleteConfirmation }, //what you send to the php
success: function(data){ //answer from your php
alert(data);
},
dataType: "text"
});
},
Javascript:
if( confirm('Are you sure?')){
/*proceed?*/
}
这是标准的javascript。它将 AJAX 调用包装在大括号内,如果用户单击 'yes' - ajax 调用将执行。如果他点击 'no',则什么也不会发生。 confirm()
提示会在屏幕上弹出带有按钮的警报。结果由 if
捕获。您还可以捕获结果:var res=confirm('are you sure?');
.
我想让用户点击按钮确认删除记录,然后按钮将值 Yes 发送到 $_POST 请求,这样我就可以使用 PHP 删除记录,而不是它向我展示了包含函数的 javascript 文件。
我哪里做错了,我一直在修补和搜索 3 个小时,而在我的脑海里,我知道这很容易解决。
非常感谢任何帮助。
如果您认为有更聪明的方法来执行此操作,请告诉我。
$(function () {
$("#DeleteConfirm").dialog({
buttons: {
"Confirm": function() {
var DeleteConfirmation = "Yes";
$.ajax({
type: "POST",
url: "index.php",
data: DeleteConfirmation,
success: function(DeleteConfirmation){
alert(DeleteConfirmation);
},
dataType: "text"
});
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
});
您需要将 YES
值分配给 属性 名称:
data: { "confirmed": DeleteConfirmation},
然后在您的 PHP 中,您可以使用 $_POST["confirmed"]
准备好它。
然而,这似乎有点多余,因为用户已经单击按钮以确认删除,因此在 POST 请求中发送值似乎没有什么价值。发送要删除的项目的 id
会更有意义。
您的 ajax 格式错误,请尝试:
$.ajax({
type: "POST",
url: "index.php",
data: {answer: DeleteConfirmation }, //what you send to the php
success: function(data){ //answer from your php
alert(data);
},
dataType: "text"
});
},
Javascript:
if( confirm('Are you sure?')){
/*proceed?*/
}
这是标准的javascript。它将 AJAX 调用包装在大括号内,如果用户单击 'yes' - ajax 调用将执行。如果他点击 'no',则什么也不会发生。 confirm()
提示会在屏幕上弹出带有按钮的警报。结果由 if
捕获。您还可以捕获结果:var res=confirm('are you sure?');
.