0

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?

1
  • 1
    You already answered the question in your title: AJAX, e.g: api.jquery.com/jquery.ajax Commented Nov 5, 2014 at 20:02

2 Answers 2

1
$(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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

See: http://api.jquery.com/jQuery.post/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.