1

Just getting to grips with this jquery and ajax thing.

I want to run a small script on a page and I've gathered I need to use POST from a jquery function to do this. I'm having trouble sending querystrings along though, what am I doing wrong?

$.post("inventory.php?use=" + drag_item.attr('id'));

the drag_item.attr('id') is a small one word text, is that the right way of doing it?

2 Answers 2

1
$.post("inventory.php?use=" + drag_item.attr('id')); //wrong

this is wrong, it takes an additional set of params for this purpose:

$.post("inventory.php",{use:''+drag_item.attr('id')},function(responseData){
    //callback goes here
});
Sign up to request clarification or add additional context in comments.

Comments

1

You should encode the parameters:

$.post("inventory.php", { use: drag_item.attr('id') });

Also in this example you are only sending an AJAX request but never subscribing to any success callback in order to process the results returned by the server. You could do this like that:

$.post("inventory.php", { use: drag_item.attr('id') }, function(result) {
    // this will be executed when the AJAX call succeeds and the result
    // variable will contain the response from the inventory.php script execution
});

Also make sure that the drag_item that you are using in this example has been properly initialized to some existing DOM element and that this DOM element has an id attribute.

Finally use a javascript debugging tool such as FireBug in FireFox or Chrome developer toolbar in Google Chrome to debug your AJAX requests and see the requests and responses being sent to and from the server and any possible errors that might occur in between.

2 Comments

thanks, so I can use a normal $_GET[use] method on inventory.php to work with the data yeah? You lost me with all that DOM talk, but I guess I'll work it out.
@user1022585, no you cannot use $_GET["use"] in your server side script because you are not sending a GET request. You are sending a POST request from the client, so you should use $_POST["use"] on the server to fetch the corresponding value. Or modify your client script to use $.get instead of $.post.

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.