PHP - Form error handling issue
error-handling, forms, html, php
Solution
Here is some rewriting of your work with a minimal handling of error messages.
BTW, you should consider adopting a decent PHP framework which will help you to handle a lot of common development tasks.
$name = '';
$surName = '';
$email = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
$surName = $_POST['surName'];
$email = $_POST['email'];
$errors = array();
if ($name == '') { $errors[] = "Please type your name."; }
if ($surName == '') { $errors[] = "Please type your surname."; }
if (!checkEmail($email)) { $errors[] = "Wrong email format."; }
if (count($errors) == 0) {
// tip: use PDO or mysqli functions instead of mysql ones to bind variables.
// currently there is a risk of SQL injection here
mysql_query("INSERT INTO USR_INFO (NAME, MAIL, SURNAME)
VALUES ('$name', '$email','$surName') ") or die(mysql_error());
header('Location: thanks.php');
exit;
}
}
if (count($errors) > 0)
echo '<p>Sorry, there are problems with the information you have provided:</p>';
foreach($errors as $error)
echo '<p class="error">'.$error.'</p>';
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
<label for="name">First Name</label>
<input type="text" name="name" id="name" value="'.htmlspecialchars($name).'" />
<span class="required">*</span>
<label for="surName">Last Name</label>
<input type="text" name="surName" id="surName" value="'.htmlspecialchars($surName).'" />
<span class="required">*</span>
<label for="email">E-mail</label>
<input type="email" id="email" name="email" placeholder="example@domain.com" value="'.htmlspecialchars($email).'" />
<span class="required">*</span>
<input type="submit" name="submit" id="submit">
</form>';
Problem
I am trying to create error messages if certain conditions aren't met. So the user fills out a form and if a field is empty or doesn't pass my validation it returns the error message. This is the form: ``` if (isset($_POST)) { if (checkEmail($email) == TRUE && $name != NULL && $surName != NULL) { mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) VALUES ('$name', '$email','$surName') ") or die(mysql_error()); header('Location: thanks.php'); } else { echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST"> <label for="name">First Name</label> <input type="text" name="name" id="name" value="' .$_POST['name'].'" /> <span class="required">*</span> <label for="surName">Last Name</label> <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" /> <span class="required">*</span> <label for="email">E-mail</label> <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" /> <span class="required">*</span> <input type="submit" name="submit" id="submit"> </form>'; } } else { echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST"> <label for="name">First Name</label> <input type="text" name="name" id="name" value="" /> <span class="required">*</span> <label for="surName">Last Name</label> <input type="text" name="surName" id="surName" value="" /> <span class="required">*</span> <label for="email">E-mail</label> <input type="email" id="email" name="email" placeholder="example@domain.com" value="" /> <span class="required">*</span> <input type="submit" name="submit" id="submit"> </form>'; } ``` So what I tried is adding an array to display the error messages like so: ``` $errorMessage = array(); ``` And add this to the html form field with the proper message: ``` $error[] = "Error Message"; ``` Now what I am stuck with is that I want to have the error show only if a user doesn't meet the conditions ``` if ($name == NULL) {$error[] = "Error Message";} if ($surName == NULL) {$error[] = "Error Message 2";} if (checkEmail($email) == FALSE || NULL) {$error[] = "Error Message 3";} ``` But I can't make it work. When I tried to implement this logic it will parse the page fine and the validation works as well but the error messages wont show up if I leave a required field blank. My guess is that I didn't loop through it properly. Help is much appreciated! EDIT: I tried the answer that was posted by Frosty Z and this is what I have at the moment: ``` if (isset($_POST)) { $errorMessage = array(); if ($name == '') { $errors[] = "Input name please." } if ($surName == '') { $errors[] = "Input last name please." } if (!checkEmail($email)) { $errors[] = "Email address not valid." } if (count($error) == 0) { mysql_query( "INSERT INTO USR_INFO (NAME, MAIL, SURNAME) VALUES ('$name', '$email', '$surName') ") or die(mysql_error()); header('Location: thanks.php'); exit; else { if (count($errors) > 0) echo "<p>Sorry, there are problems with the information you have provided:</p>"; foreach($errors as $error) echo '<p class="error">'.$error.'</p>'; echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST"> <label for="name">Name</label> <input type="text" name="name" id="name" value="' .$_POST['name'].'" /> <span class="required">*</span> <label for="surName">Last name</label> <input type="text" name="surName" id="surName" value="' .$_POST['surName']. '" /> <span class="required">*</span> <label for="email">E-mail</label> <input type="email" id="email" name="email" placeholder="example@domain.com" value="' .$_POST['email']. '" /> <span class="required">*</span> <input type="submit" name="submit" id="submit"> </form>'; } } else { echo'<form action="<?php echo $_SERVER[\'PHP_SELF\']; ?>" method="POST"> <label for="name">Name</label> <input type="text" name="name" id="name" value="" /> <span class="required">*</span> <label for="surName">Achternaam</label> <input type="text" name="surName" id="surName" value="" /> <span class="required">*</span> <label for="email">E-mail</label> <input type="email" id="email" name="email" placeholder="example@domain.com" value="" /> <span class="required">*</span> <input type="submit" name="submit" id="submit"> </form>'; } ``` With this my page won't be parsed. I have error reporting on but it doesn't show anything besides a Internal server error 500 in my console log(Firebug)