javascript - 使用 Ajax POST PHP 和 javascript 数据

标签 javascript php jquery mysql ajax

如果这是一个重复的问题,我深表歉意,但我在其他地方找不到答案。

我有一个页面,用户可以在其中查看所有提交的内容,每个提交内容都位于单独的一行中。有一个“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/

相关文章:

php - 使用 jQuery、Ajax 和 PHP 的投票系统只能在本地服务器上运行

javascript - 使用 jQuery 删除 20 后的 div

php - 随机连接MySQL

php - Mysql COUNT,未按预期计数

javascript - 使用 jquery 隐藏带有名称的元素

数据库查询后jQuery不可拖动

javascript - jQuery 在 smarty 代码上抛出 Uncaught Error

javascript - 修改脚本使其更像实际的横幅 slider

JavaScript - 将 JavaScript 与 HTML 连接起来

php - 将德语单词存储到 MySql 数据库中时出现问题....?