簡體   English   中英

使用Ajax發布PHP和javascript數據

[英]POST PHP and javascript data using Ajax

如果這是一個重復的問題,我深表歉意,但在其他任何地方都找不到答案。

我有一個頁面,用戶可以在單獨的行中查看所有提交的信息。 有一個“ X”,當用戶單擊它時,我希望在數據庫中刪除該行。

這是用戶單擊“ X”時觸發的函數(jQuery)

$(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.
            }
    });
});

參數:

網址:自我說明

dataType:您期望從PHP腳本獲取的數據類型

方法:用於請求的http方法

data:您將傳遞給PHP的數據,因為在這種情況下,我們使用POST。 如果您使用的是GET,則可以像在其他任何url上一樣在“ url:”字段中設置變量。

成功:請求成功后會發生什么。

$.post('/some-file.php', {id: e.target.id});

參見: http : //api.jquery.com/jQuery.post/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM