- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Home › Forums › Back End › How do I keep or retain my form values even if the form has been submitted?
I have a form that submits its values to itself. I want the values to stay printed so the user will not try to right all over again what he/she has previously typed.
When you receive the form submission, save the valid values and print them when you print the form again.
# check if form was submitted
if( isset( $_POST ) ){
# validate submitted value
$text = some_func_to_validate( $text )?
$text:
null;
}
# if $text was submitted & valid,
# use it for form value
$text_value = $text?
$text: // submitted value
“”; // empty string
# print form
print <<
Thanks @traq for that. I just wonder, Should it output any error if there is no validation used? I got the idea from your code. I just used the isset to check if the form was submitted then declared my variables and used them as field value.
Will I encounter any problems with this?
if( isset( $_POST ) ){
$title = $_POST;
$editor_data = $_POST[ ‘editor1’ ];
$format = $_POST[ ‘format’ ];
}
my form:
Have you thought about using localStorage to save form values?
> Should it output any error if there is no validation used? *[…]* Will I encounter any problems with this?
At a **minimum**, you need to be sure you don’t output any HTML or JavaScript with the values (use [`strip_tags()`](http://php.net/strip_tags) or [`htmlentities()`](http://php.net/htmlentities)) as a safeguard against XSS attacks.
> Have you thought about using localStorage to save form values?
— @JohnMotylJr
I agree that it’s good to use localstorage *also*, but doing it in PHP is still practical (since you have to do server-side validation anyway) and convenient for the user (since you return *only* the valid values).