There are two files

index.php , server.php

In the index.php file

There is a list

 <select size="3" name="abonents" id="abonents"> <option value="2938">2938</option> <option value="3781">3781</option> <option value="3591">3591</option> </select> 

Block

 <div id="block"></div> 

Ajax request

 <script> $(document).ready(function(){ $("#abonents").click(function(){ var a = $("#abonents").val(); $.post('server.php', {id:a}, function(data){ $("#block").text(data); }); }); }); </script> 

When you click on the list, the value is transferred by the POST method to the server.php file and the data from the Ajax request is supplied to the block

File server.php

 <?php if (isset($_POST['id'])){ // ... // Запрос к БД, возвращет массив $result // ... echo print_r($result); } ?> 

Currently, the data is displayed as text

How can I transfer the Ajax request data ( Data value) to a PHP array for further processing in the index.php file?

  • In the index.php itself, it index.php not work. You can return the values ​​in the javascript function that sent the ajax request. - Pyramidhead
  • @Pyramidhead but you can in more detail - Xfirab
  • And why not pass to index.php ? - Xfirab
  • one
    Because at the time of sending ajax request, index.php has already completed. Actually, the result of his work is the page that he brought. - Pyramidhead
  • in the server file, make echo json_encode($result) , and handle ajax, and get it back var values = JSON.parse($data) - teran

2 answers 2

The $result array, which you output using the print_r() function, must be converted to JSON, or XML, or to any other format you wish and output.

 echo json_encode($result); 

You may need to specify other parameters of the json_encode() function.

Also, before that, it would be nice to indicate that you are returning json data, use the headers for this:

 header('Content-Type: application/json'); 

On the index.php side, convert the result of the ajax request back to an array using JSON.parse() .

 $.post('server.php', {id:a}, function(data){ var arrayData = JSON.parse(data); // и далее обработать массив значений. }); 

Return the result and pass it for further processing in the PHP-code file index.php not work. Since the file has already executed this code, and the page shown is the direct result of its work. Therefore, you have two paths. Implement the logic of the received data using JS . Or contact server.php directly in the code of index.php itself. There may be several solutions to the problem depending on where these files are located. Perhaps if on a single server, you can make include desired file, or if this option is not suitable, then create a GET request using curl or file_get_contents() functions and wait for it directly during the execution of index.php .

in the latter case, you get the same array of values ​​encoded in JSON format, and decode it back:

 $result = json_decode(file_get_contents('/server.php',.....)); foreach($result as $r){ ... } 

Regarding the formation of the remaining file_get_contents parameters for transmitting a POST request and parameter substitution, this is a separate issue that was recently discussed here.

It remains to note the obvious option, when the server.php file gives you not an array of data, but the generated html-code, which you display on your page. This is the most common way to work with ajax.

  • I wanted to process the resulting array with PHP tools, alas, I’ll have to try using JS :-( - Xfirab
  • @Xfirab I updated the answer. If you need to work with an array in pkhp, then there is no sense to request data through JS. - teran
  • I just wanted to, when choosing from the list without reloading the page, get the array of data from the database - Xfirab
  • @Xfirab then update my answer again :) server.php not return an array from server.php , but form the html-code directly there that needs to be displayed. If you have access to this file of course, and you can edit it. - teran
  • teran just accurately noticed that the generated html-code is transmitted. Thanks for the clarification) - Xfirab
  <?php header('Content-type: application/json'); if (isset($_POST['id'])){ /// echo json_encode($result); } ?> js: var values = JSON.parse(data)