Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

Return Multiple Values From .post() ?

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

New User

5+ Year Member

joined:Jan 12, 2012
posts: 38
votes: 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:

debug: false,
submitHandler: function(form) {
// do other stuff for a valid form
$.post('process.php', $("#myform1").serialize(), function(data) {
setTimeout(function() {
}, 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
2:23 pm on Feb 1, 2012 (gmt 0)

Senior Member from ES 

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month

joined:July 24, 2002
votes: 2

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...]