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

Asked

Viewed 2,780 times

-1

Can you return values from a PHP page via Ajax and display them separately? Ex. I have a PHP page on 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 certain DIV, only that they all appear together (in the same place). I would like to know if I could determine which "$a" will be displayed in a particular DIV, and the other variables in other DIV. It’s like?

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

You can do just that?

Thanks in advance.

  • Why don’t you put your code in the question? So it would be less trouble for anyone who would be interested in helping you, as well as giving you a response adjusted in your own code.

2 answers

1

As you are working with Javascript try returning your PHP response in JSON format to 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;

    }
});

0

In php, I put in an array the 3 variables(a,b,c),no js da pra saber pelo Indice qual é, variavel A é Indice = 0,so on. Done this Voce will manage to display each one 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];
    }


});
  • My friend, your answer has helped me a great deal, but I have not yet succeeded. I believe it may be something related to the ECHO command in an ARRAY, as it does not convert it cannot pass the parameters. I tried with PRINT_R but nothing happens either. Could you please help me?

  • good observation, corrected code.

  • Thanks for your attention, but still not displaying the values. I did the test with both: Document.write(a); "as well as $("#a"). append(a); and none of them displays the value.

  • Voce changed the ajax to its url ( url: 'taa.php', ) ? I ran tests and no problem occurred.

  • taa.php ? No! I kept my UL ( url: 'server.php',). How you displayed the values in your tests ?

  • js : https://jsfiddle.net/goqhfsng/ php : http://sandbox.onlinephpfunctions.com/code/b5a9c416ab36b63f273de41997ffebba743ec2e4

  • Strange, the one you posted is also not working here... Obs: my PHP variables are strings and integers and my jquery function is called by a button: $("button"). click this may interfere with the final result ?

  • opens the console of your browser, checks if it comes to make any request for your file . php

  • Nothing buddy. I ended up finding a palliative alternative. In the PHP file I took the ARRAY and used the IMPLODE function to pass the values through the URL as string: $txt = implode("|", $vector); In ajax I used dataType as HTML, normal even: dataType: "html" and gave a SPLIT in the string that came in the UL parameter: result = array.split("|"); Then it was just display what I wanted: in array[0], array[1], array[2]... Like I said, this was a palliative form, which already covers my problem. But I would really like to understand the problem with JSON, because I’ve needed it other times and I haven’t.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.