Reputation: 647
I am doing server side validations for my html values. However when server side validation fails and I return back to the page, the form data is cleared. Is there anyway to do save and reload the form data in php without using any framwork?
Upvotes: 0
Views: 1399
Reputation: 16495
Just put the submitted value (if there is any ) inside your value=""
like this:
$set = isset($_POST['username']) ? $_POST['username'] : '';
The above is called the ternary operator. And, as I have done it, it will check if someone has already submitted a form with attribute "username" ($_POST['username']
) in your form, if yes, then it sets the the value of that form to a variable called $set
otherwise it just sets it ''
nothing.
Now, all you have to do is just put the $set
variable inside like: value=" <?= $set ?> ">
Upvotes: 1
Reputation: 13766
Well, sure. Assuming that you're submitting to a different script, and need to be directed back to your page with the form, use PHP Sessions. Save all of the data that you need to reload in the session, and then on your page use the session data to pre-populate the form if it's there. Once you're done using it (i.e. they've successfully completed their form), then you can clear the session on a subsequent interaction.
Upvotes: 1
Reputation: 836
Send the values to the server using an AJAX call, that wont refresh the page leaving your data intact in the page.
Upvotes: 0
Reputation: 3337
Yes,
Say you have your input:
<input type="text" name="email" />
If you did this:
<input type="text" name="email" value="<?php echo $_POST['email']?>"/>
It would repopulate with whatever they typed in.
Upvotes: 0