Search code examples
phpjqueryajaxreturn-value

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?


Solution

  • 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);
    });