Hello, you need to transfer json string from one php file to another. In the first file I wrote the following code:

$json = array("item" => "dress", "id" => "353" ); echo json_encode($json); 

In the second file I try to catch the string through JS and display it in the browser when I click on the button:

 <button id="showTableBtn">SHOW TABLE</button> <div id="table"></div> <script> $('#showTableBtn').click(function() { $.getJSON("file1.php", function(data){ $("#table").html(data.item); }); }); </script> 

But when you click on the button does not display anything. Tell me, please, what am I doing wrong?

  • one
    What kind of errors in the console? - rjhdby
  • one
    There are no errors in the console - Yumie
  • one
    request errors? on the network activity tab, the query returns the correct data? - teran
  • in network activity, the request returns the page itself, i.e. file1.php - Yumie
  • one
    @Yumie forgot to write <?php at the beginning of the file?) - Alexey Shimansky

1 answer 1

Everything is working. You are using the getJSON method, and it belongs to jQuery connected it and it all worked. For reliability, I indicated in the headers that the data format is JSON, but it also worked without it.

 <html> <head> <script src="https://code.jquery.com/jquery-3.2.1.js"></script> </head> <boby> <button id="showTableBtn">SHOW TABLE</button> <div id="table"></div> <script> $('#showTableBtn').click(function() { $.getJSON("file1.php", function(data){ $("#table").html(data.item); }); }); </script> </boby> </html> 

well, and php

 <?php header('Content-Type: application/json'); $json = array("item" => "dress", "id" => "353" ); echo json_encode($json); ?>