Receive real time variable ajax

Asked

Viewed 579 times

3

Well I’m making a system, and I need to receive a php page variable every time, so I can work on it on my index.php page.

A friend of mine who understands more of ajax than I gave me the following code:

However, I don’t know how to pass the values of variable x in PHP (loading.php), for my Javascript on the page index.php.

The code of my index.php page is like this:

<html>
    <head>
        <script src="jquery-3.0.0.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            $.get("loading.php", function(data) {
                alert(data);
            });
        </script>
    </body>
</html>

How can I do?

  • The PHP page loading.php has to be somewhere echo 'Alguma coisa...'; and so ajax picks up that string and sends it to callback. You know how to do that?

  • From PHP, I mean a lot, so tell me one thing, if I want to receive 2 variables instead of one, I would echo 2x of the 2 variables, but then how I received them in javascript?

  • In this case it sends an array or an object. echo json_encode(array('foo', 'bar'));and in Javascript: JSON.parse(data); or using a jQuery method for JSON.

  • Could you give me an example? , I’m a little new at this ajax and json.

2 answers

2

Good here would be an example of sending data by ajax

  $.ajax({
              var dado= "algumas coisa que queira enviar";
              url: "recebe.php",

              type: 'POST',

              data: dado,

              success: function (data) {
               $("body").html(data);



              }
            });

Ai in php that receives this data, which in this case is the receive.php, which receives this data through the POST method, would look like this.

 <?php
    $DadoRecebido= $_POST['dado'];
    echo "deu certo";
?>

And ajax over there would write in the "It worked" body, which is the echo I gave in php that received the ajax data. I hope I’ve helped

  • In case, I wanted to receive the data from the page and not send! But thank you very much for the help, I was very grateful and I will use this in the future.

2


If you want to pass several values I suggest you send a PHP JSON to Javascript.

JSON is an organized data string and in PHP you can create JSON of arrays or objects with json_encode($aMinhaVariavel);

To make AJAX collect that data you have to use echo.

So on the PHP side you can have for example like this:

$array = array('dados A', 'dados B');
echo json_encode($array);

In Javascript you can use $.getJSON() jQuery which already converts the JSON string.

And then you can have it like this:

$.getJSON("loading.php", function(array) {
    alert(array[0]);
    alert(array[1]);
});
  • In this case, if you wanted to pass this data from array[0] for a variable, could do as follows: var goncalo = array[0]; it is certain?

  • @Exact Gonçalo.

  • 1

    I did it! Thank you Sergio!

  • @Gonçalo great. I’m glad I could help.

Browser other questions tagged

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