homepage Welcome to WebmasterWorld Guest from 50.17.79.35
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Return Multiple Values From .post() ?
ZakAltF4




msg:4411562
 6:05 pm on Jan 27, 2012 (gmt 0)

I am updating an older script that I have .. I am now trying to pull multiple bits of data from a php page that is posted to using jquery. Here is my current code:


$(document).ready(function(){
$("#myform1").validate({
debug: false,
submitHandler: function(form) {
// do other stuff for a valid form
$.post('process.php', $("#myform1").serialize(), function(data) {
$('#results').fadeIn('fast');
$('#results').html(data);
setTimeout(function() {
$('#results').fadeOut('slow');
}, 2500);


});
}
});
});


What I am looking to do is pull two to three DIFFERENT variables from this page. I am not sure this is at all possible, I read about .post and can only find it returning .data which is the HTML output of the entire script. What if I wanted to return:
1) success,success update message, success html
2) database error (duplicate), fail update message, fail update html
3) database error, (unknown database error - returned value), fail update html

My PHP will return either 1, 2, or 3. Either way it will return 3 variables. How do I pass this to my JQuery that originally posted the form? Is it at all possible?

-- Zak

 

jamie




msg:4413065
 2:23 pm on Feb 1, 2012 (gmt 0)

hi zak,

put the php results in array format, then you can use json_encode() on it and return that.

you may find it easier to use $.ajax() instead of $.post() whilst learning as it forces you to write the parameters in longhand.

as long as you set the dataType as 'json', the data object which is returned is a json object. so if your original php array was array('result' => 'OK') then data.result contains OK.

the api docs explain it quite well: [api.jquery.com...]

hth

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved