Reputation: 1405
I have a simple question regarding multiple ajax calls, performing each call in sequence. I have a button that posts the current page's form (which will write the form values into session), then I want to call another page (GET) after these session variables are set.
The problem is, I run both calls in a function and if the first post does not make it through in time, the session values are not present on the other script which are required.
Here's my button's function:
function generateFP() {
$.post("test.php", $("#webcto_form").serialize());
loadPage('getpage.php?page=fpreq','page_contents');
}
And my loadPage function:
function loadPage(filename,id){
$("#page_contents").load(filename);
}
Is it possible to use the shorthand ajax ($.post/.load) in conjunction with a success? I want to change my generateFP to call the loadPage function after successfully submitting the form.
I've seen standard $.ajax documentation explaining how to build a success function but have not yet found any information using the syntax that I am using.
Upvotes: 0
Views: 604
Reputation: 1181
This should work for you. You will be calling the loadPage function in a success event handler function.
function generateFP() {
$.post("test.php", $("#webcto_form").serialize(), function(data){
//this is the success callback function
//data parameter holds the response returned from the post if you need it.
loadPage('getpage.php?page=fpreq','page_contents');
});
}
Upvotes: 2
Reputation: 5637
The jQuery Post method allows a success handler (http://api.jquery.com/jQuery.post/):
function generateFP() {
$.post("test.php", $("#webcto_form").serialize(), function(){
loadPage('getpage.php?page=fpreq','page_contents');
});
}
Alternatively, you could modify your test.php script to return you the information you are after from your second request:
function generateFP() {
$.post("test.php", $("#webcto_form").serialize(), function(data){
console.log(data);
});
}
Where data
is the output of your test.php script.
Upvotes: 4