I send a request to the server

<html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <form action="http://0.0.0.0:1234/qwerty" method="POST"> <input type="hidden" name="command" value="cmd"> <input type="text" name="fio"> <p><input type="text" name="usl"></p> <input type="submit" value="Отправить"> </form> <?php ?> </body> </html> 

After clicking on the "Send" button, the answer comes in the form of a json-string on a new page. Using php you need:

  1. Send a request so that when the answer comes (json-string) stay on the page with the form.
  2. Write the json string to a variable and parse; or parse immediately, if possible, in order to later output part of the data to the intended form field.
  • jQuery ajax . The variable will come back. - Artem Gorlachev
  • @ Nataliya To stay in that page there are 2 options, or you must use the action form to leave the current page and process the post request in it, or make a post request using the ajax request. - Raz Galstyan
  • @RazmikGalstyan if I remove the server address in action, then where can I register it so that the request is processed? Ajax I do not know. - Nataka
  • @Natalia In the same file, then requests should be processed, pkhp a piece of request processing code should be in this very file. - Raz Galstyan
  • @Natalia And one more thing, let us specify in the question where do you want to parse the json string? You wrote it like this И как записать эту json-строку в переменную, чтобы потом распарсить? . Explain in what language you want to parse the string? - Raz Galstyan

2 answers 2

Here is an example of your code where I tried to explain how you can implement this without ajax:

 <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <?php if(isset($_POST['command'])){ // Здесь ваша логика сервера которую нужно будет писать в тот же файл // $tmp_field который вы хотите вставить в форму } ?> <form action="" method="POST"> <input type="hidden" name="command" value="cmd"> <input type="text" name="fio"> <p><input type="text" name="usl" value="<?php echo isset($tmp_field) ? $tmp_field : ''; ?>"></p> <!--Пример где вставляеться переменная--> <input type="submit" value="Отправить"> </form> </body> </html> 

Pay attention to a few things:

If the form does not prescribe action, it means that the action is the same URL. The line if(isset($_POST['command'])) means that we came to our url using the post method (click on submit in the form). And there we are already doing the processing of our data and in the variables we will save the values ​​we need. And this is the value="<?php echo isset($tmp_field) ? $tmp_field : ''; ?>" submit then these variables will not be declared and we must check their existence before using them in our code.

    Use AJAX for this purpose.

    For quick implementation, you can use the jQuery library

     <html> <head> <meta charset="UTF-8"> <title></title> <script type="text/javascript" scr="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script type="text/javascript"> jQuery(document).ready(function($) { $.ajax({ url: 'http://0.0.0.0:1234/qwerty', type: 'POST', dataType: 'json', data: { command: $('input[name="command"]').val(), fio: $('input[name="fio"]').val(), usl: $('input[name="usl"]').val(), }, }) .done(function(data) { console.log("success"); }) .fail(function() { console.log("error"); }) .always(function() { console.log("complete"); }); }); </script> </head> <body> <form action="http://0.0.0.0:1234/qwerty" method="POST"> <input type="hidden" name="command" value="cmd"> <input type="text" name="fio"> <p><input type="text" name="usl"></p> <input type="submit" value="Отправить"> </form> </body> 

    in the data variable of the done function will be your answer from the server