使用jQuery AJAX从PHP返回多个值

时间:2022-09-25 15:56:01

I'm using this jQuery code:

我使用的是jQuery代码:

$.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?

我如何做这样的事情:$(".projDesc").html(html1);所以我可以把返回的结果分成两个html元素?

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

这就是我正在使用的PHP,我想要回显另一个语句,我可以将它放入另一个HTML元素中

Does this make sense?

这是否有意义吗?

3 个解决方案

#1


55  

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

使用json_encode()将关联数组从PHP转换为JSON,并使用$. getjson(),它将返回一个Javascript数组。

Example:

例子:

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

In Javascript:

在Javascript中:

$.getJSON("myscript.php", function(data) {
  alert("Value for 'a': " + data.a + "\nValue for 'b': " + data.b);
});

#2


42  

Make your response return JSON, you'll need to change your jQuery to this, so the expected dataType is json:

让您的响应返回JSON,您需要将jQuery更改为这个,因此期望的数据类型是JSON:

$.ajax
({
    type: "POST",
    url: "customerfilter.php",
    dataType: 'json',
    cache: false,
    success: function(data)
    {
        $(".custName").html(data.message1);
        $(".custName2").html(data.message2);
    }
});

Then you need to encode your response as a JSON Array:

然后需要将响应编码为JSON数组:

 <?php echo json_encode(
      array("message1" => "Hi", 
      "message2" => "Something else")
 ) ?>

#3


3  

Why don't you return a JSON object. This way you can easily put many different results inside the ajax response.

为什么不返回一个JSON对象呢?通过这种方式,您可以很容易地将许多不同的结果放入ajax响应中。

#1


55  

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

使用json_encode()将关联数组从PHP转换为JSON,并使用$. getjson(),它将返回一个Javascript数组。

Example:

例子:

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

In Javascript:

在Javascript中:

$.getJSON("myscript.php", function(data) {
  alert("Value for 'a': " + data.a + "\nValue for 'b': " + data.b);
});

#2


42  

Make your response return JSON, you'll need to change your jQuery to this, so the expected dataType is json:

让您的响应返回JSON,您需要将jQuery更改为这个,因此期望的数据类型是JSON:

$.ajax
({
    type: "POST",
    url: "customerfilter.php",
    dataType: 'json',
    cache: false,
    success: function(data)
    {
        $(".custName").html(data.message1);
        $(".custName2").html(data.message2);
    }
});

Then you need to encode your response as a JSON Array:

然后需要将响应编码为JSON数组:

 <?php echo json_encode(
      array("message1" => "Hi", 
      "message2" => "Something else")
 ) ?>

#3


3  

Why don't you return a JSON object. This way you can easily put many different results inside the ajax response.

为什么不返回一个JSON对象呢?通过这种方式,您可以很容易地将许多不同的结果放入ajax响应中。