Answer the question
In order to leave comments, you need to log in
How to force automatic execution of ajax+php script?
Good afternoon.
There is a form
<form method="post" id="ajax_form">
<label>Numb: 1234567890</br></label>
<input type = 'hidden' name='numb' value='1234567890'>
</form>
<div id="result_form"><div>
$(document).ready(function(){
sendAjaxForm('result_form', 'ajax_form', 'action_ajax_form.php');
setInterval('sendAjaxForm()',2000);
});
function sendAjaxForm(result_form, ajax_form, url) {
$.ajax({
url: url, //url страницы (action_ajax_form.php)
type: "POST", //метод отправки
dataType: "html", //формат данных
data: $("#"+ajax_form).serialize(), // Сеарилизуем объект
success: function(response) { //Данные отправлены успешно
result = $.parseJSON(response);
$('#result_form').html('Получено число: '+result.numb);
},
error: function(response) { // Данные не отправлены
$('#result_form').html('Ошибка. Данные не отправлены.');
}
});
}
<?php
if (isset($_POST["numb"]) {
// Формируем массив для JSON ответа
$result = array(
'numb' => $_POST["numb"]
);
// Переводим массив в JSON
echo json_encode($result);
}
?>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question