How to save the entered data in the browser when unsuccessful sending to the sql database, well, or so that the information in the form does not disappear after the page is updated?
3 answers
In any case, option one is to save the form information on the client side.
Alternatively, you can use jQuery .keyup () in conjunction with localStorage / sessionStorage (cookie can be used at least).
In the example below, code using localStorage and the jQuery library.
$(document).ready(function() { var text = $('#text'); if (localStorage.getItem('text')) text.val(localStorage.getItem('text')); text.keyup(function() { localStorage.setItem('text',text.val()); }); });
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> </head> <body> <input type="text" id="text" /> </body> </html>
Cookies Make an onKeyup event on input, etc. Immediately write to cookies. When refreshing the page, we check if there is a cookie with the value of a specific input - if there is, then the input value is assigned to the value from the cookie
- interesting option. Only probably
onBlur
better ... Although it depends on the project ... But the option is VERY interesting! - cyadvert
There are two options for this.
More simple, but a little less successful, is called Post / Redirect / Get - output data immediately upon receipt. That is, in case of “unsuccessful sending”, we simply output the form again, and enter the obtained values into it. after successfully adding data, we redirect to the same URL.
example
if ($_SERVER['REQUEST_METHOD']=='POST') { // обрабатываем полученные переменные. // пишем в переменную $err ошибки } if (!$err) { save(); // сохраняем редиректим Header("Location: ".$_SERVER['REQUEST_URI']); exit; } } // показываем страницу с формой. // значения заполняем из $_POST
The second is a bit more complicated, but more correct in terms of usability. We write the received data into the session, put the error messages there, redirect, display the completed form and errors.
session_start(); if ($_SERVER['REQUEST_METHOD']=='POST') { // обрабатываем полученные переменные. // пишем в переменную $err ошибки } if ($err) { $_SESSION['err'] = $err; $_SESSION['post'] = $_POST; } else { save(); // сохраняем редиректим } Header("Location: ".$_SERVER['REQUEST_URI']); exit; } if (isset($_SESSION['post'])) { $err = $_SESSION['err']; $post = $_SESSION['post']; unset ($_SESSION['err'], $_SESSION['post']); } // показываем страницу с формой. // значения заполняем из $post
- These two methods do not struggle with the following case. The user begins to enter data and presses F5. - KoVadim 2:21 pm
- @KoVadim; You can think of many possible cases. The question specifically concerns the error in data validation. I used a redirect to the form page in my project. Only passwords are erased. - ArchDemon
- TS wrote explicitly "so that the information in the form does not disappear after the page is updated" - KoVadim
- Can you provide any link with an example of the second method? it looked like everything in the search engine was vaguely written everywhere - Alex Prosto
localStorage
:) htmlbook.ru/html5/storage Works on the client side, as on this site. - Visman