There is a code to retrieve data from the json file, and to populate the table with data where, next to each line, there is a delete button, the id of which I receive and try to send this id to the post with a request to the server so that it in turn deletes the entry from the json file.

$.getJSON('table_push.json', function(data) { for (var i = 0; i < data.length; i++) { $('#users').append('<tr id=' + data[i].id + '><td>' + data[i].id + '</td><td>' + data[i].firstName + '</td><td>' + data[i].secondName + '</td><td>' + data[i].email + '</td><td><button data-id=' + data[i].id + ' class="btn btn-danger">Remove</button></td></tr>'); } $(".btn-danger").bind('click', function () { var line = $(this).data('id'); // тут id получаю, уже проверял его значение в консоли, всё верно $('#' + line).remove(); //тут прото скрываю строку в таблицу remove = 'remove='+line; //ну это переменная с id тоже выводится в alert правильное значение $.ajax({ // вот тут я отправляю id на сервер url: "core.php", type: "POST", dataType: "text", data: remove }); }); }); 

Below is the php handler code:

 $idToDelete = filter_var($_POST["remove"],FILTER_SANITIZE_NUMBER_INT); //Удаляет все символы, кроме цифр и знаков плюса и минуса $delete = mysqli_query($CONNECT, "DELETE FROM `test` WHERE id`=".$idToDelete); // тут уже обращение к БД и удаление данных по полученному id $goJson = 'writeJson'; $goJson();//вызываем функцию записи в json файл 

So, firebug shows the execution of the POST request as soon as I click on the button to remove some of the lines, and the value that is sent, that is, the id number. But deletion from the database does not occur. I have an assumption that the ajax request to the server cannot be contained inside the $ .getJSON function, but the request passes, and beyond the limits of this function, I can not get the value of the variable in order to execute the ajax request outside of it. The server should also take on the values, because data is added to the database, then something happens but there is no deletion.

  • You can always check whether your request was fulfilled or not - if(!$delete){echo mysqli_error($CONNECT);} - Alex
  • error in the request. 1) there is not enough backward quotes in the WHERE for id ; 2) in a good way, the values ​​need to be framed in quotes, i.e. The query must be "DELETE FROM test WHERE id = '$ idToDelete'" , where test and id must be in backquotes. - Alex
  • For future, check sql queries before adding to the code, for example via phpmyadmin or via the console mysql client. - fens
  • @fens is optional (see first comment). - Alex
  • @Alex but not necessarily, but the response there is faster. And as a bonus, the new version of pma has support for autocompletion. - fens

0