PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Yes I have caused myself yet another head ache. Anyways what I have included in my web form is some simple error checking using 'if' statements. However when there is a value missed out it echos the message onto a new page. How can I get my error messages to appear on the same page somewehere specific??
//Error Checking for certain form fields that are required.
if (empty($_POST['fname'])) { $errors[] = 'Please enter your first name';}
if (empty($_POST['lname'])) { $errors[] = 'Please enter your last name';}
if (empty($_POST['fromemail'])) { $errors[] = 'Please enter your email address';}
else if (!eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_POST['fromemail'])) { $errors[] = 'Please enter a valid e-mail address';}
if (empty($_POST['location'])) { $errors[] = 'Please enter your place of residence';}
if (empty($_POST['notice'])) { $errors[] = 'Please enter your required period of notice';}
if (empty($_POST['current_salary'])) { $errors[] = 'Please enter your current salary';}
if (empty($_POST['working_location'])) { $errors[] = 'Please enter your current working location';}
if (empty($_POST['strengths'])) { $errors[] = 'Please enter your strengths';}
if (empty($_POST['qualifications'])) { $errors[] = 'Please list your qualifications';}
if (empty($_POST['comments'])) { $errors[] = 'Please add some additional comments that could strengthen your application';} else if (strlen ($_POST['comments']) > 255) { $errors[] = 'Your comment is too long';}
if (count($errors) == 0) {
}else{ echo $errors[0];}
//Error Checking End
what do you mean it shows them on a new page? The data has to be posted to this page in order for your validator to run...just output the results from your array here.
Sorry I should have been more clear with this problem. If you miss out a field in the form it echos a message but it clears the page and displays said message at the top of a blank page.
What I want to do is return the errors below each input field in a <?=$error;?> type format. Or even in a table data box. I just want to have control over where the error messages will be displayed. Any idea?
User submits form ->
PHP if statements check for errors ->
if an error is found, I add a message onto a variable called $errors that explains the error. I also set a variable called 'hasErrors' to true (it's set to false to start with).
After all the error checking, I do an if statement to check if hasErrors is true. If it's false, I allow the data to be inserted and redirect the user to a new page. If it's true and there were errors, I send them back to the original form, with the fields populated with whatever they typed. I also assign the $error variable to a session, and then at the top of the form I echo the session variable, so they can see their error at the top right away.
mattpointblank wrote:The way I do this sort of thing is like so:
User submits form ->
PHP if statements check for errors ->
if an error is found, I add a message onto a variable called $errors that explains the error. I also set a variable called 'hasErrors' to true (it's set to false to start with).
After all the error checking, I do an if statement to check if hasErrors is true. If it's false, I allow the data to be inserted and redirect the user to a new page. If it's true and there were errors, I send them back to the original form, with the fields populated with whatever they typed. I also assign the $error variable to a session, and then at the top of the form I echo the session variable, so they can see their error at the top right away.
Make sense?
Yeah that makes a good amount of sense, thank you for that. Could I ask for a code example if possible?
Sure. This is cut down from the (more complex...) version, so it's not tested, but should work when configured properly (eg, add your form, change the variable names, add/remove validation tests etc.
<?php
$errors = "";
$hasErrors = false;
session_start();
if(isset($_POST['addArticle'])) {
// apply clean() function to all $_POST variables to protect against sql injection
$cleanvars = array_map('clean', $_POST);
// extract all the cleaned up $_POST variables to new variables, prefixed with article_
// (eg, a form field called 'title' is now $article_title, and is cleaned up
extract($cleanvars,EXTR_PREFIX_ALL,'article');
// loop through the form input and make sure there was a value for all of them
foreach($cleanvars as $value) {
if((!isset($value) || $value == "")) {
$hasErrors = true;
$errors .= "<li>Make sure you've filled out all of the fields.</li>\n";
break; // break here so that we don't get the above error multiple times if they left multiple fields blank
}
}
if(!is_numeric($article_price)) { $hasErrors = true; $errors .= "<li>Please make sure the price is a number.</li>\n"; }
// end of error checks, so assign the $errors variable to a session
if($hasErrors) { $_SESSION['response'] = $errors; }
// only add the article if there's no errors
if ($hasErrors == false) {
// code to add the articles goes here
header("Location: success.php"); // send them somewhere else
} else { // there was an error, so send them back
session_write_close(); // included to fix weird php bug that wipes the session
header("Location: originalpage.php"); // send them back the original page
exit();
}
}
?>
<!-- PUT YOUR HTML AND FORM CODE HERE -->
<?php
// this displays the errors
if (isset($_SESSION['response'])) {
echo $_SESSION['response'];
}
?>