Reputation:
I have had a look at other posts but could not solve the issue.
I am trying to find what is the easiest way to keep form values after post. For example a registration form,
If a user enters all correct data except for one field (an email address for example) then the form gets submitted via a PHP form, how do I keep the valid form data but get rid of the data that is not valid?
Below is my form code:
<form action="./scripts/register.php" method="POST" name="registration" />
Your Name:
<input class="submissionfield" type="text" name="name" required pattern="[a-zA-Z\s]+" title="Your name can only contain alphabetic characters" placeholder="Bob Smith">
Username:
<input class="submissionfield" type="text" name="username" required pattern="[a-zA-Z0-9]+" title="Username can only contain alphabetic characters and/or numbers. (No Spaces)" placeholder="bsmith29">
Email:
<input class="submissionfield" type="email" name="email" required placeholder="[email protected]" title="Example email address: [email protected]">
Password:
<input class="submissionfield" type="password" name="password" required pattern="[a-zA-Z0-9]+" title="Password can only contain alphabetic characters and/or numbers. (No Spaces)" placeholder="Enter Password Here">
Age:
<input class="submissionfield" type="number" required pattern="[0-9]" title="Please enter your age, only numeric characters are allowed " name="age" required placeholder="Select your age > > ">
Are you Male or Female?
<input type="radio" name="gender" value="male" checked> Male
<input type="radio" name="gender" value="female"> Female
What gender are your dogs?
<input type="checkbox" name="gender_of_cats[]" value="male" checked> Male
<input type="checkbox" name="gender_of_cats[]" value="female"> Female
<div class="registration_form_complete">
<input id ="registration_buttons" type="submit" value="Submit">
</div>
</form>
Upvotes: 1
Views: 173
Reputation: 550
There are several ways. Using AJAX API calls for validation allows you to notify the user something is wrong with a field before the form is posted. This is by far the most user friendly option.
If you really want to POST and let the server do all the validating in one go, there are 2 options. The main problem is that a POST will reload the page. So you need your server to respond to the POST with the new form-page, and fill in the validated values on the server side (so the user gets back the filled-in form.
The second way is to store the values in localstorage
when you POST, and load them in again using javascript when you reload the page. However, in that case you don't get the validated values directly from the server, so I would not recommend this method.
Upvotes: 1
Reputation: 2408
You can try
<input class="submissionfield" type="text" name="name" value="<?php echo $_POST?$_POST["name"]:""; ?>" required pattern="[a-zA-Z\s]+" title="Your name can only contain alphabetic characters" placeholder="Bob Smith">
Upvotes: 0
Reputation: 128
You need Javascript for this. After the form submit the page will reload, so any information is lost by default. To store the values you could create a cookie before submiting the form and load these values onLoad() into the fields.
Upvotes: 0