I have some html forms that I verify completeness with php. The issue I have is that when one required form is not filled, the filled forms are cleared.
The HTML
<p>Email: <span class="required">* <?php echo $EmailErr;?></span><input type="text" name="Email" placeholder="Email" /></p>
<p>Comments: =input type="text" name="Comments" maxlength="75" placeholder="Comments"/></p>
This is the PHP
if (empty($_POST["Email"])) {
$EmailErr = "";
} else {
$Email = validateEmail($_POST["Email"]);
}
if (empty($_POST["Comments"])) {
$Comments = "";
} else {
$Comments = test_input($_POST["Comments"]);
}
The question remains, how do I prevent the other forms from being cleared upon submission?
You should do a client side validation in order to retain the values on your form.
That being said you should still have server validation.
There is different way to do it, using javascript/jquery or even simply adding required
attribute to your tag, for example:
<input type="text" name="Email" placeholder="Email" required/>
for javascript:
http://www.w3schools.com/js/js_validation.asp
for jquery , here is a good plugin:
<p>
Email: <span class="required">*<?php echo $EmailErr; ?></span>
<input type="text" name="Email" placeholder="Email" value="<?php echo!empty($_POST['Email']) ? htmlspecialchars($_POST['Email']) : '' ?>"/>
</p>
<p>
Comments: <textarea name="Comments" maxlength="75" placeholder="Comments"><?php echo!empty($_POST['Comments']) ? htmlspecialchars($_POST['Comments']) : '' ?></textarea>
</p>