Hello everyone, please tell me how to display records from the MySQL БД table without reloading the page? Suppose if a new line has been added to the БД , then I will display it on the page without reloading. Throw off a good article where the implementation is described or maybe you have a simple example to see and understand at least the essence of how it should work.
- This will depend on a lot, but since you gave PHP, here is one example - socketo.me/docs/push - Daniel Protopopov
2 answers
There are 2 main approaches:
1) polling - poll the server (for example, using ajax) once in a period of time and display new records from the database if they appear there. This approach has several drawbacks: - the data does not appear instantaneously, but only the next time the server is polled - a large load on the server - more traffic from the client partially 2 last drawbacks can be reduced by using HTTP caching headers
2) maintain a permanent connection to the server and from the server side send data immediately after adding it. For this you can use, for example, websocket. This solution is somewhat more difficult to implement, but has no such disadvantages as polling.
- Thanks for the answer, how difficult is the implementation of the second option, and are there any simple examples to get acquainted first? - Ruslan Liashenka
- for PHP it is not very easy to do because of its specificity. But if you quickly google you can find something like socketo.me - dizballanze
Here is a working example of ajax implementation using jquery index.php file:
<?php function is_ajax() { return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'; } if (is_ajax() && isset($_POST['get_data'])) { $pdo = new PDO("mysql:host=localhost;dbname=DBNAME;charset=utf8", 'root', 'PASSWORD'); $pr = $pdo->prepare("SELECT * FROM `main` "); $pr->execute(); $data=$pr->fetchAll(); echo json_encode(array( 'ok' => 'AJAX OK!!!', 'data'=>$data )); exit(); } ?> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> </head> <body> <a id="get_data">Получить данные в браузер</a> <script> $("document").ready(function () { $("#get_data").click(function (event) { event.preventDefault(); var data = {"get_data":1}; $.ajax({ type: "POST", dataType: "json", url: "index.php", data: data, success: function (r) { console.log(r); // r будет содержать "ответ" из PHP } }); return false; }); }); </script> </body> </html> Using echo json_encode(array('ok' => 'AJAX OK!!!')); we send the answer to our browser and in the developer panel we can see the answer.
It should be noted that passwords and important data are usually passed by the post method.
Use for example PDO to fetch your data from mysql or oracle : D then you get an array and you can send this data array to the browser without reloading the page.
- The bottom line is to send data using the POST or GET methods (I recommend POST) using JS without reloading and catching it in PHP, and then sending the response to the browser. - fonjeekay
- thanks for the answer, but the question was slightly different, you send data to the database without rebooting, but you need to do a sample without rebooting - Ruslan Liashenka
- I changed the example. I hope that is clear. Here is an article on AJAX. w3schools.com/xml/ajax_intro.asp as for PDO, that is a lot of information on php.net - fonjeekay
- Using echo json_encode (array ('ok' => 'AJAX OK !!!')); we send the answer to our browser and in the developer panel we can see the answer. This response may contain an array of data, which can then be used to change something in HTML using just
javascriptor `jquery. two lines do not explain. you need to start with connecting to the database to understand the essence. The code that I presented is a very small example. - fonjeekay - well, thanks more, I will read, try, I hope everything will work out - Ruslan Liashenka
