如果这是一个重复的问题,我深表歉意,但我在其他地方找不到答案。
我有一个页面,用户可以在其中查看所有提交的内容,每个提交内容都位于单独的一行中。有一个“X”,当用户单击它时,我希望在数据库中删除该行。
这是我的函数(jQuery),当用户单击“X”时触发
$(document).ready(function(){
$(".delete").click(function(e){
var id = e.target.id;
$("#"+id + "_row").hide('slow');
//Need to POST the id variable to PHP file.
});
});
如何将 ID 变量发布到 php 文件,然后在数据库上运行 mySQL 查询?
最佳答案
$(document).ready(function(){
$(".delete").click(function(e){
var id = e.target.id;
$("#"+id + "_row").hide('slow');
$.ajax({
url: "path/to/php/file",
dataType: "json",
method: "POST",
data: { target: e.target.id },
success: function(data) {
// do something with the data passed back by the PHP script.
}
});
});
参数:
url: self explanatory
dataType: the kind of data you're expecting to get from the PHP script
method: http method which you'll use for your request
data: data you'll be passing to the PHP, since we're using POST in this case. If you were using GET, you can set variables in the "url:" field just like you would on any other url.
success: what happens when the request succeeds.
关于javascript - 使用 Ajax POST PHP 和 javascript 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26765993/