There is a request of the form

UPDATE `gb_ptich` SET `pay_status` = REPLACE(`pay_status`, '0', '1') 

you need to run it when clicking on the link <a href="#">Обновить</a>

How to do this through PHP?

  • You make a button, hang up a handler, send a request. What are the difficulties? - Mr. Black
  • if I could, I would not ask, the scheme is clear, but thanks for the answer - lxxnutsxxl
  • Actually, it was a question. Ie, what exactly is not clear - Mr. Black
  • Specifically, code example is needed = ( - lxxnutsxxl
  • one
    So full of examples around. You can make a form in html with the request. You can send a get or post request using ajax or xhr. Based on the title of the question, you want to create a handler for pressing the button and sending the request, and in the tags I see only php and mysql. So what exactly is required? - Mr. Black

1 answer 1

You can do the following: let the form be located in the index.html file and contain the client part of the gadget (link and AJAX binding for it)

 <!DOCTYPE html> <html lang='ru'> <head> <title>Обновление таблицы</title> <meta charset='utf-8'> <script type="text/javascript" src="https://code.jquery.com/jquery-2.2.4.min.js" > </script> <script type="text/javascript"> // Назначить обработчики события click // после загрузки документа $(function(){ $("a").on("click", function(){ // Отправляем AJAX-запрос $.post("handler.php"); }) }); </script> </head> <body> <a href="#">Обновить</a> </body> </html> 

As you can see, the AJAX request is sent to the file handler.php, which may contain the following code, to execute the UPDATE request (using the PDO extension)

 <?php try { $pdo = new PDO( 'mysql:host=localhost;dbname=test', 'root', '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); $query = "UPDATE `gb_ptich` SET `pay_status` = REPLACE(`pay_status`, '0', '1')"; $pdo->exec($query); header('location: /'); exit(); } catch (PDOException $e) { echo "Ошибка выполнения запроса: " . $e->getMessage(); } ?> 
  • Thank you, everything is there, earned =) - lxxnutsxxl
  • @Doofy, I agree, I did not carefully read, adjusted the client part to the minimum AJAX. - cheops
  • one
    @cheops, but often the author can not ask the right question what he needs. And those who answer questions, you see, telepaths. Or the author is just lazy. Please write me this function, and I will tick the answer . And while you find out from the author what he needs, others are already rushing to answer - Mr. Black
  • @cheops, even if the answer is designed as it should be. Separate at least php from html. Since jquery was connected, it was possible to do it in one line through $.post - Mr. Black
  • one
    @Doofy, thanks for the comment, corrected the answer. - cheops 7:37 pm