Good afternoon, gentlemen, there is a ready-made code written in php, which displays the data in an html table:

<tbody> <tr> <?php foreach ($data as $values): ?> <?php $hyperkey = $values['list_project_id']; ?> <?php foreach ($values as $value) : ?> <td><a href="/project/<?= $hyperkey ?>"><?= $value ?></a></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> 

I want to update my spreadsheet when clicking a button, and AJAX is the best solution. I started like this:

 function fucnSuccess(data){ $('tbody').html(data) } $(document).ready(function(){ $("#reload").click(function(){ $.ajax({ type: "GET", url: "server.php", success: fucnSuccess }) }); });` 

The beginning seems to be understandable, we hook onto our AJAX on the button, But what to do with php data, translate into JSON? How to implement it?
And what if you need to update the data in two pages, that is, the table is such that when I click on a record, it loads information (also from Mysql) about this record

  • Simply put, js can be left as it is, and the request can be made on the same php where the generated html will be - Artem Gorlachev
  • is it possible in more detail? - King of Night

1 answer 1

If you transfer all the logic of drawing html to js, ​​then it is approximately like this:

php:

 echo json_encode($data); 

js / jQuery:

 function fucnSuccess(data) { var rows = ''; for (var i in data) { var hyperkey = data[i].list_project_id; var row = '<tr>'; for (var v in data[i]) { row += '<td><a href="/project/' + hyperkey + '">' + data[i][v] + '</a></td>'; } row += '</tr>' rows += row; } $('tbody').html(rows) } 

And you can leave your php as it is, make a request to the same php where we are and update the table like this:

 function fucnSuccess(data){ var tmp = $('<tmp>').html(data); $('tbody').html(tmp.find('tbody').html()); } 
  • that is, if the option is with php, then I need to "throw all the logic of drawing" to where I turned to the database and form requests, and make the output as return $ myTBODY - King of Night
  • I suggested the option not to touch the existing php at all, just to pull the whole page again and on the client update only the contents of tbody - Artem Gorlachev
  • I understood you about the update, but I still do not understand what will happen in the "data". - King of Night
  • in the data will be the entire html page - Artem Gorlachev
  • when adding an entry to the html table itself, use the append method. - King of Night