JQuery $ .post Returns

With jQuery $ .post, can I return data to an array or return 2 data sets?

Example:

$.post("MyScript.php", { action:"run" },function(data){
    alert(data);
});

Now this simple post above sends an action to a php script that performs 2 functions and returns data, which is then displayed in a warning window. But my PHP script has 2 functions and should do different things with each of the returned data.

So, essentially, I am asking if $ .post can return 2 datasets, for example:

$.post("MyScript.php", { action:"run" },function(data1, data2){
    alert(data1);
    $("div1").html(data2);

});

or can the return data be an array, where can I assign values ​​to the elements of the array in a php script?

I hope this makes sense.

+3
source share
4 answers

data... , HTML, XML, JSON ..... JSON , :

$.post("MyScript.php", { action:"run" },function(data){
  alert(data.property1);
  $("div1").html(data.property2);
});
+3

100%, , , JSON object. datastructure php , , JSON. JSON - , . jQuery, json , json $.post. , jQuery json true javascript object.

Ref.: $. post, JSON

+2

MyScript.php, . , json, xml .. . .;)

. json- ( jquery), - :

$.post("MyScript.php", { action:"run" },function(data){
    $.each(data, function(index, value)
    {
        alert( index+' : '+value );
    });
});

Take a look here: http://api.jquery.com/jQuery.post/ . They have a few examples that may help you .; D

+1
source

As soon as the data returns only one, I found that it worked for me.

The contents of myfile.php were sent to:

<table>
  <tr><td>
    <h2>My Heading here</h2>
    <div id="mydiv">something here</div>
  </td></tr>
</table>

JQuery in the file with the form for publishing:

$.post("myfile.php", { txtField1: "whatever" },
        function (data) {
            var content = $(data).html();
            $("#submit_result").html(content);
        }
 );

This outputs the entire table and its html contents in #sumbit_result.

+1
source

Source: https://habr.com/ru/post/1780419/


All Articles