N
N
Newn2018-04-19 13:10:36
PHP
Newn, 2018-04-19 13:10:36

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>


In which there is a hidden field and will store a certain number. It is necessary to automatically transfer this number to the ajax script after loading or in time, through which the php script will process it.

ajax.js
$(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
<?php

if (isset($_POST["numb"]) { 

  // Формируем массив для JSON ответа
    $result = array(
    	'numb' => $_POST["numb"]
    ); 

    // Переводим массив в JSON
    echo json_encode($result); 
}

?>


I understand little and know little js, ajax, jquery. Tried to follow the examples. So, I would be grateful for specific answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey0502, 2018-04-19
@Newn

index.html
load.php
5ad8792989376065149877.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question