Hello. Help, please, in solving the next question. It is necessary to send data to the server via ajax, on the server using php to process and return it. I made an ajax request, but the server gives error 502. Denver is installed. I do not understand how my php script will listen to the request. Interested in the moment of interaction. Tell me where you can read, or advise the book. There are many request processing scripts in the internet. But how php heard the request, received and returned the answer.
1 answer
For example, something like this:
index.php
<?php if (isset($_GET['ajax'])) { $text = (!empty($_POST['mytext'])) ? 'MD5: <b>'.md5($_POST['mytext']).'</b>' : 'Текст не передан'; echo $text; exit(); } ?> <html> <head> <title>TEST AJAX</title> <script src="https://code.jquery.com/jquery-3.1.0.min.js"></script> </head> <body> <div> <input type="text" id="mytext" placeholder="Введите какой-нибудь текст"> <button type="button" id="send-mytext">SEND</button> </div> <hr> <div id="result"> </div> <script> $(document).ready(function(){ $('#send-mytext').click(function(){ var blockResult = $('#result'); $(blockResult).html('Продождите...'); $.ajax({ url: '/index.php?ajax', type: "POST", data: ({ mytext: $('#mytext').val() }), success: function( answer ){ $(blockResult).html( answer ); } }); return false; }); }); </script> </body> </html> But I would recommend you to study this topic in more detail.
|