There is a GetSelect method - I will get a list of categories from my "id, name" database.

function GetSelect($link) { $query = "select categories.id, categories.name from categories"; $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, ['id'=>$row[0], 'name'=>$row[1]]); } // for $json_res = json_encode($json_result); file_put_contents('../json/categories.json', $json_res); mysqli_free_result($result); } // if } 

Everything is fine, JSON data is successfully saved. enter image description here

But now I cannot display them in the select list "value - key". The script is written - sense zero. The list is empty. What to do?(

 <script> $.getJSON('../json/categories.json', function(data) { // Очищение списка $('#mySelect').empty(); for (var i = 0; i < data.length; i++) { $('#mySelect').append('<option value="' + data[i].id + '">' + data[i].name + '</option>'); } } ); </script> 

Added: FILE PASSED VALIDATION! enter image description here

  • Maybe because you are sending a list? In the plan you sent [{}] , not {} - users
  • Suppose this is a problem, how to avoid it? - Kryshtop
  • users is right. You have not valid JSON. Wrap the entire output in braces and it will work. $ json_res = json_encode ($ json_result); replace with $ json_res = "{" + json_encode ($ json_result) + "}"; - XelaNimed
  • I can not complain about commas then in json file (data transfer) - Kryshtop
  • Everything works as it should, I put the file in the same directory as the HTML file. ( pastebin.com/DDeJpekV ) - Daniel Protopopov

1 answer 1

The problem was solved by demolishing phpstorm.

  • already can not joke. - Kryshtop
  • For reference: here the site is not anecdotes (joking is the maximum in the comments), and the subject answers, useful to a wide circle of readers. - ߊߚߤߘ