Question

When someone do not fill all fields in my formular the site is showing error and it is refreshing so that someone must fill it again.

How to make the site remember correctly filled fields?

Was it helpful?

Solution

When the form is submitted, then on page refresh the $_POST data is still intact. So what you can do is something like this:

<input type="text" name="field_name" value="<?php echo (isset($_POST['field_name']) ? $_POST['field_name'] : ''); ?>">

What it does is that if the $_POST data for the field field_name is set, then echo its value.

OTHER TIPS

Use of autocomplete can preserve your previous inputs.

Autocomplete ON will preserve the value and OFF will prevent the input box from remembering. In sample code, email have off as autocomplete.

<input autocomplete="on|off">

OR

<form action="/action" autocomplete="on">
  First name:<input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  E-mail: <input type="email" name="email" autocomplete="off"><br>
  <input type="submit">
</form>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top