Je suis novice en PHP, et je cours le soir. Nous avons un projet pour une validation de formulaire. J'ai le code suivant, mais quand je clique sur submit, il redirige simplement vers le site désiré, sans faire la validation requise.La validation de formulaire PHP ne fonctionne pas correctement
<?php
$checkedMale = $_POST['gender'] == 'Male' ? "checked='checked'" : '';
$checkedFemale = $_POST['gender'] != 'Male' ? "checked='checked'" : '';
$formValidates = false;
if(isset($_POST['submit']) && $_POST['submit'] == 'Register') {
$errors = array();
if ($_POST['firstName'] == '') {
$errors[] = '<p>Please fill in your first name</p>';
}
if ($_POST['surname'] == '') {
$errors[] = '<p>Please fill in your surname</p>';
}
if ($_POST['email'] == '') {
$errors[] = '<p>Please fill in your e-mail address</p>';
} else {
if (! filter_var ($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$errors[] = "<p>Please supply a valid e-mail address</p>";
}
}
if ($_POST['address'] == '') {
$errors[] = '<p>Please fill in your postal address</p>';
}
if (count($errors)== 0) {
$formValidates = true;
}
}
if (! $formValidates) {
// Displays errors
if (count($errors) > 0) {
echo "\n<ul>";
foreach ($errors as $error){
echo "\n\t<li>$error</li>";
}
echo "\n<ul>";
}
?>
<form name="questions" action="lesson8_1.php" method="post">
<table>
<tr>
<th>Title:</th>
<td>
<select name="title">
<option value="">Select</option>
<option>Mr</option>
<option>Mrs</option>
<option>Miss</option>
<option>Dr</option>
</select>
</td>
</tr>
<tr>
<th>First name:</th>
<td><input type="text" name="firstName" placeholder="First Name..." value="" /></td>
</tr>
<tr>
<th>Surname:</th>
<td><input type="text" name="surname" placeholder="Surname..." value="" /></td>
</tr>
<tr>
<th>Email:</th>
<td><input type="text" name="email" placeholder="E-mail Address..." value="" /></td>
</tr>
<tr>
<th>Address:</th>
<td><textarea name="address" placeholder="Postal Address..."></textarea></td>
</tr>
<tr>
<th>Gender:</th>
<td>
<input type="radio" name="gender" value="Male" <?php echo $checkedMale?> >Male<br>
<input type="radio" name="gender" value="Female" <?php echo $checkedFemale?> >Female<br>
</td>
</tr>
<tr>
<th></th>
<td>
<input type='checkbox' name='option[]' value='Car'
<?php echo in_array('Car', $_POST['option']) ? 'checked' : '' ?>>I have a Car licence<br>
<input type='checkbox' name='option[]' value='Motorcycle'
<?php echo in_array('Motorcycle', $_POST['option']) ? 'checked' : '' ?>>I have a Motorcycle licence<br>
<input type='checkbox' name='option[]' value='Fishing'
<?php echo in_array('Fishing', $_POST['option']) ? 'checked' : '' ?>>I have a Fishing licence<br>
<input type='checkbox' name='option[]' value='TV'
<?php echo in_array('TV', $_POST['option']) ? 'checked' : '' ?>>I have a TV licence<br>
<input type='checkbox' name='option[]' value='Dog'
<?php echo in_array('Dog', $_POST['option']) ? 'checked' : '' ?>>I have a Dog licence<br>
</td>
</tr>
<tr>
<th></th>
<td><input type="submit" name="submit" value="Register" /></td>
</tr>
</table>
</form>
<?php } else { ?>
<h1>Your form has been successfully submitted!</h1>
<?php } ?>
La balise ouverte
rechercher d'autres exemples de la façon de soumettre un formulaire php ** après ** validation http://stackoverflow.com/questions/3206166/validate-form-before-submitting-more-complicated-than-checking-for -empty-fields –