Welcome to WebmasterWorld Guest from 50.19.156.19

Forum Moderators: open

Message Too Old, No Replies

Return Multiple Values From .post() ?

     

ZakAltF4

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

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

WebmasterWorld Senior Member 10+ Year Member



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month