简体   繁体   中英

POST PHP and javascript data using Ajax

I apologize if this is a duplicate question, but I couldn't find the answer anywhere else.

I have a page where users are able to see all of their submissions with each one in a separate row. There is an "X" that, when users click it, I want that row to be deleted in the database.

Here is the function that I have (jQuery) that triggers when the user clicks the "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.
    });
});

How would I post the ID variable to a php file that would then run the mySQL query on the database?

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

Parameters:

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.

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

参见: http : //api.jquery.com/jQuery.post/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM