This seems like it should be a simple answer if you know Jquery AJAX. This script works but it returns the response of the query string of 3 xhr fields sent. Instead of the query string response pair results returned from response I just want value 1. {task_todo:value1, task_person:value2, due_date: value3}.

This script returns all 3 values
function submitForm() {

					 $.ajax({type:'POST', url: 'form_handler.php', 
					          data:$('#taskForm').serialize(),
					          success: function(response) {
						  var qs = response;
                            $('#taskForm').find('.taskList').html(response);
                     }});
                     return false;
                 }

I tried to figure out how to parse out the response query string value below by adding the expressions below but couldn't get the first
value. Could you tell me what I'm doing wrong? THANKS. 😕😕:confused

var task_todo= $.query(qs)['task_todo'];
alert(task_todo);
alert(qs[0].split)"=")[1]);

    Parse it in JavaScript, or parse it in PHP? Either way, it's just JSON, and both have functions for parsing it.

      I tried to parse the query string using Javascript below and added the split function. I added the alert to see what pair[1] would be and I get undefined. Is pair[0] the whole query string. Should pair[1] be the first value. As I said the response gives all 3 values in the query string and I only want the 1st value. What is wrong with the parsing code I added between the asterisks. THANKS.
      function submitForm() {

      					 $.ajax({type:'POST', url: 'form_handler.php', 
      					   		data:$('#taskForm').serialize(),
      					        success: function(response) {
                                                       *******
      						    var qs = response;     
      							    var vars = qs.split("&");
      							    for (var i = 0; i < vars.length; i++) {
      							        var pair = vars[i].split("=");
      							        if (pair[0] == qs) {
      							            alert( pair[1]);
      							        }
      							    }
      		          *****
      
                                  $('#taskForm').find('.taskList').html(response);
                           }});
                           return false;
      
                       }

        Perhaps I'm not understanding the problem, but in your $.ajax call you should be specifying what type of data you'll be getting back (jQuery will guess but it's nice to be specific).

        Add...

        dataType : 'json'
        

        ...to your $.ajax call. Then, in your PHP script, place your data into an associative array then call the json_encode() function to return the data.

        Now you can access the data in your JavaScript by using JSON notation. So far example something like "response.task_todo".

          thank you so much for the jquery data type correction , sorry this question was not specific, my main problem was not knowing how to get the separate form values from the form from the jquery response because it gets all values and parsing it in javascript is a bit unwieldly. I couldn't figure out where to put another function in the jquery function. I will try your suggestion.

            Hi, I did exactly what you said but something is not quite right with the change to the response function. I added the json dataType. I changed the form_handler.php page to return the POST assoc array of the fields. In the AJAX response in firebug I see the associative array fields I want and the status 200 so the php page checks. Now how to change the Jquery AJAX response function to give me the field I want. The asterisks are I think where the error is. TIA

            This is how I guessed you meant.

            				 
            									 $.ajax({
            								type: 'POST',
            
            							url:  'form_handler.php', 
            							dataType : 'json' ,
            
            					   		data: $('#taskForm').serialize(),			
            					        success: function(response) {
            						    var task = response.task_todo;     ***
            				           alert(task);                                           ****
            
                                   $('#taskForm').find('.taskList').html(task);
                                     }
                              });
                                 return false;
                              } 
            

              Can you post your PHP code where you're echoing back the data?

                Bonesnap;11000744 wrote:

                Can you post your PHP code where you're echoing back the data?

                Here is the AJAX response:
                Array
                (
                [task_todo] => laundry
                [todo_name] => janis
                [due_date] => 5/25/2012
                )

                Here is the php form handler:

                if($_POST['task_todo']) {
                          $ary = $_POST;
                json_encode($ary);		
                
                print_r($ary);
                
                  }
                

                tia

                  jrough;11000785 wrote:

                  Here is the php form handler:

                  if($_POST['task_todo']) {
                            $ary = $_POST;
                  json_encode($ary);		
                  
                  print_r($ary);
                  
                    }
                  

                  This isn't going to return a json_encoded array, instead it will print the word Array (try it yourself). json_encode's parameter isn't passed by reference, therefore the array passed to it isn't changed rather the function returns the encoded array. Also why use all this anyway? All this could be simplified to:

                  print( json_encode( $_POST ) );

                    This statement:

                    json_encode($ary); 

                    doesn't do anything. Meanwhile, this statement:

                    print_r($ary); 

                    is not going to output the array as a JSON encoded string (which is what jQuery is expecting).

                    EDIT: Woops - guess I need to refresh old tabs after I go get a coffee and before replying. :o

                      Write a Reply...