On the Internet I find the implementation of ajax requests only with forms, I can look badly. I need that I would click on the link in index.php, the link leads to a handler in which the data is entered into the database. And this data was loaded into index.php without reloading the page. How to implement it correctly?
1 answer
index.php
<a href='#' id='test'> SendData </a> js (jQuery):
$(document).ready(function(){ $('#test').on('click', function(e){ $.ajax({ url: "Обработчик.php", datatype: "json", cache: false, async: true, contentType: "application/json; charset=utf-8", method: "POST", data: { // ... данные которые отправляем на сервер (например id нажатой ссылки) 'id':$(this).attr('id') }, success: function (data, textStatus) { //... тут ответ с данными от сервера(например результат select from BD), делаем с ними что захотим } }); }); Handler.php (catching data by the key 'id'):
<?php $link = $_POST['id']; // тут с БД работаем // ... // возвращаем данные js скрипту (в "success") echo json_encode(массив с данными); ?> |