Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple return values from PHP with jQuery AJAX

I'm using this jQuery code:

$.ajax ({     type: "POST",     url: "customerfilter.php",     data: dataString,     cache: false,     success: function(html)     {         $(".custName").html(html);     } }); 

How can i do something like this: $(".projDesc").html(html1); So i can split the returned results into two html elements?

echo "<p>" .$row['cust_name']. "</p>"; 

thats the PHP i'm using and i want to echo another statement which i can put into another HTML element

Does this make sense?

like image 772
benhowdle89 Avatar asked Jan 04 '11 13:01

benhowdle89


1 Answers

Use json_encode() to convert an associative array from PHP into JSON and use $.getJSON(), which will return a Javascript array.

Example:

<?php echo json_encode(array("a" => "valueA", "b" => "valueB")); ?> 

In Javascript:

$.getJSON("myscript.php", function(data) {   alert("Value for 'a': " + data.a + "\nValue for 'b': " + data.b); }); 
like image 77
Klemen Slavič Avatar answered Oct 13 '22 02:10

Klemen Slavič