Return PHP values via Ajax and display in HTML (separately)

0

How can I return values from a PHP page via Ajax and display them separately? Ex.: I have a PHP page in which I have the return values:

<?php
 echo $a;
 echo $b;
 echo $c;
?>

Currently I can determine via Jquery that they are displayed in a given DIV, but that all appear together (in the same place). I would like to know if I would have to determine how "$ a" will be displayed in a given DIV, and the other variables in other DIVs. How?

My purpose is: Access the database, bring values and set in a modal. Currently I can already call this MODAL and fill her BODY with the data from the server. However, I do not want to display these values only in BODY. I need to display $ a on the HEADER, $ b on the BODY and $ c on the FOOTER of this MODAL.

Can you do just that?

Thank you in advance.

    
asked by anonymous 03.12.2017 / 16:47

2 answers

1

As you are working with JavaScript try to return your PHP response in JSON format for AJAX:

<?php
    echo json_encode(
        "a" => "1",
        "b" => "2",
        "c" => "3",
    ));
?>

And now you can work with this return as follows:

$.ajax({
    url : 'seu-arquivo-php',
    success : function(data) {
        var json = $.parseJSON(data)[0];
        var a = json.a;
        var b = json.b;
        var c = json.c;

    }
});
    
13.12.2017 / 21:06
0

In php, I put in an array the 3 variables (a, b, c), in the js of knowing by the index what is, variable A is index = 0, and so on. Once you have done this you will be able to display each separately.

<?php
// echo $a;
// echo $b;
// echo $c;
   $arr = [$a,$b,$c];
   echo (json_encode ($arr));

Ajax:

$.ajax({
    method:'post',
    url: 'taa.php',
    async: true,
     dataType:'json',
    success: function (arr) {
        var a = arr[0];
        var b = arr[1];
        var c = arr[2];
    }


});
    
03.12.2017 / 17:47