How can I send a PHP variable on one page to another Javascript page using Ajax?

Asked

Viewed 326 times

0

Hello, people, I’d like to know how to send a php variable to another javascript document through Ajax.

The PHP variable in question is this:

$json = json_encode(simplexml_load_string($show));

Here it returns me the value of a string, but I saw that Json has a Json.parse function, which converts all this to objects, so it is easier to manipulate to make a table, however, I want to make things separate, php page receives these values and javascript sends it to html where the user will see his registration, if, there is an easier and safer method I also accept, in general, send this variable to a JS/Json page and there I will make the table with the information

  • 1

    Supplement your question with the full code

1 answer

1


What you are being part of the concepts used in a REST API. See here

Backend is in charge of managing and providing the data in a practical format like json, from there, the responsibility of displaying this data is the application of Frontend ( or other backend in case of Microservices).

An example with jQuery:

var objetoJS = jQuery.parseJSON(jsonDoServidor)

jsonDoServidor is what was generated by your json_encode(simplexml_load_string($show));

Note: This should be routed to your JS in some way, either via common http (in the API Routes style) or even in rendering (not the best practice anymore works).

If you are going to do it via rendering, when your PHP generates HTML, you can do something by embedding PHP into JS. Example:

index.html

<script> 
var jsonDoServidor = jQuery.parseJSON(
      <?php echo json_encode(simplexml_load_string($show)); ?>
)
console.log(jsonDoServidor)
</script>

Note that your PHP is creating jsonDoServidor dynamically to be used by JS. This works, however, perhaps not the best practice.

A better alternative is to create a file/route that returns only JSON and, through an ajax call, take this content and use it.

$.get("/linkproJson").done(function(data){
  console.log(data)
  var jsonDoServidor = jQuery.parseJSON(data)
});

No matter how you do it, you get the data in a ready-to-use Obj on JS.

I tried to explain based on what I understood, but your question was not so direct.

To help you come up with ideas, see what I have similar to your need (I use this with Laravel, so PHP tags are simplified but the concept is the same):

// Recebe o Json para a view
produtos = {!! $analises->produtos or $analisesProdutos !!} 

// Pega a tabela 
var tableRef = document.getElementById('analisesProdutos-table').getElementsByTagName('tbody')[0];

// Cria as linhas na tabela com os dados que vieram do Servidor
produtos.forEach(function (item) {
    var newRow   = tableRef.insertRow(item);
    newRow.innerHTML = `
    <td><a href="/analisesProdutos/${item.id_mlb}">${item.id_mlb}</a></td>
    <td>${item.seller_mlb}</td>
    <td>${item.name}</td>
    <td>${item.price}</td>
    <td>${item.sold_on_register}</td>
    <td id='${item.id_mlb}-sold_last'></td>
    <td></td>
    `
})

Browser other questions tagged

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