Prevent refresh after submitting on PHP pages

2

Good evening, everyone! I have a question and would like help, tips and more. I have a basic HTML form that sends the data through the POST method to the same page, ie I submit it to the page itself. Since this page has actions in Javascript, after this submit the page resets, a refresh happens, resetting checkbox for example and I would like the choices that the person made were kept. Is it possible to prevent this refresh after clicking the submit button on the form? Thanks in advance

    
asked by anonymous 27.08.2018 / 05:44

2 answers

0

The logical and easiest way is to make this post request with Ajax! Which would maintain the selection of the fields as the user chose.

But if you are submitting to the same page, you can access the values through the $ _ POST variable of php.

example test.php:

 <form action='teste.php' method='POST'>
   <input type='text' name='nome' value="<?= (!empty($_POST['nome']))? 
   $_POST['nome'] : '' ?>">
 </form>

That is, if there is a value in the variable NAME sent by POST, add it in the value attribute of the text field named name otherwise add empty

Additional content: PHP POST

    
27.08.2018 / 06:32
0

No, you can not prevent this refresh by submitting the form to the page itself.

You should make this request via ajax - it was stored in the Database but did not return anything to the html.

So you could enter the data and keep the data.

Alternative

If your goal is to maintain the data, you can check if $ _POST contains data (after doing the submit it will contain). If it contains, you can write this data in the values of the institutes of the form. In this way I would do the refresh however would fill in the fields with the values entered before submitting and that were sent.

    
27.08.2018 / 13:28