Hello. On the main page there is a script that uploads the do_rating.php file, which creates / recreates and fills the json file with data from the request.

(The file is always empty, but there is data, made a request to the workbench MySqL)

Скрипт <script> $("#myForm").submit(function(event){ event.preventDefault(); $.post('do_rating.php'); $.getJSON('../json/goods_rating.json', function(data) { // Очищение таблицы $('#resultRating').empty(); for (var i = 0; i < data.length; i++) { $('#resultRating').append('<tr><td>' + data[i].category + '</td><td>' + data[i].numbers + '</td></tr>'); } } ); }); </script> 

And here is the content of do_rating.php ...

 <?php require_once 'connection.php'; $link = mysqli_connect($host, $user, $password, $database) or die ("Не могу создать соединение"); $query = "select categories.name as category, count(goods.id) as numbers from categories left join goods on categories.id = goods.id_category group by categories.name order by numbers desc;"; $result = mysqli_query($link, $query) or die("Не могу выполнить запрос"); if($result) { $json_result = []; // массив для данных, полученных из запроса $rows = mysqli_num_rows($result); // количество полученных строк for ($i = 0 ; $i < $rows ; ++$i) { $row = mysqli_fetch_row($result); array_push($json_result, ['category'=>$row[0], 'numbers'=>$row[1]]); } // for // кодировать массив в формат JSON $json_res = json_encode($json_result); file_put_contents('../json/goods_rating.json', $json_res); // очищаем результат mysqli_free_result($result); } // if mysqli_close($link); ?> 

For other people, the code works, but I don’t. It’s still logical, the data from the json_encod request is in a json format string, saved to a file, and the main page reads this file via getJSON into the rows of the table ... but is empty ... Please help.

Added jQuery is connected, but phpstorm does not see the $ .post method, everything else perfectly understands, maybe in the interpreter case?

enter image description here

  • one
    If time is specifically the problem: data is not recorded in goods_rating.json? Or not deducted from it? - br3t
  • 2
    And yet - $ .post is asynchronous, i.e. Before reading something from goods_rating.json, you need to wait until do_rating.php writes something to it. - br3t
  • No data is written to the json file! - Kryshtop
  • one
    Then check do_rating.php: if it connects to the database, if any data is returned, there is a file at the specified path, is there write access for it ... - br3t
  • solved the problem, go here. ru.stackoverflow.com/questions/616305/… - Kryshtop

0