Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Error Processing

Featured Replies

Hey guys,

 

Up until now, I've just been using a series of if statements for each element in my form, then re-directing the user to the page showing the error that was found. However, I understand there are better ways to do this, and I want to be able to display all errors at once, rather than individually.

 

I'd liek to stop blank fields, only allow alpha-numeric characters, allow email address's, remove excess white space, prepare for entry into MySQL database. I then want to be able to display the error messages. I have already got an interface ion place for the form and error message display - just need a method of catching the errors now!

 

I'd appreciate it if someone could provide a link to a GOOD, easy to follow tutorial that explains how to do this. Or, if you have an example you've written yourself that would be great. I've got a few books but they don't explain things to well.

 

Thanks!

Put them into an array like this and spit them out at the end using a loop if validation fails.

 

// init array
$errors = array();

if (!isset($_POST['name']) {
errors[] = "Please enter your name.";
} else {
$name = trim($_PO......);
}


if (!isset($_POST['email']) {
errors[] = "Please enter your email....";
} else {
$email = trim($_PO....);
}

// Check to see if any errors occured
if(empty($errors)) {

// Process the form etc

} else {

echo "<h1>Errors</h1>";
// Print each error.
foreach ($errors as $msg) {
echo "- $msg<br />\n";
}
echo '<p>Please try again</p></div>';

 

I have attached an registration page (self submitting) which I did for a uni project. Code is based on examples from Larry Ulmans PHP 6 & MYSQL.

 

Here's an example of it working, just enter bad fields

http://www.budgetr.gemnetworks.com/index.php

register.php

Just to build on what was said in the previous post, you can unify all the required fields processing with something like this:

 

$REQUIRED = array("name","email","postcode"); // or whatever the fields are called
$reqmiss = array();
foreach($REQUIRED as $key) {
 if(empty($_POST[$key])) $reqmiss[] = $key;
}
// Specific field validation
// Rest of processing
if(count($reqmiss)>0) {
 echo "Required fields missing:";
 foreach($reqmiss as $err) echo " $err";
 echo ". Please complete all required fields.";
 // Re-display form and complete all fields with $_POST values
} else {
 // Form fields are OK so process here
}

That now checks to ensure all $REQUIRED fields are present and non-empty, and prints the error message with all field names of incomplete submissions if not. Obviously this is only a demonstration, you should do whatever you feel is necessary for your application. You would probably also want to reshow the form (automatically completing all filled-in fields), and perhaps even use a template framework rather than echos all over the place.

Just to build on what was said in the previous post, you can unify all the required fields processing with something like this:

 

$REQUIRED = array("name","email","postcode"); // or whatever the fields are called
$reqmiss = array();
foreach($REQUIRED as $key) {
 if(empty($_POST[$key])) $reqmiss[] = $key;
}
// Specific field validation
// Rest of processing
if(count($reqmiss)>0) {
 echo "Required fields missing:";
 foreach($reqmiss as $err) echo " $err";
 echo ". Please complete all required fields.";
 // Re-display form and complete all fields with $_POST values
} else {
 // Form fields are OK so process here
}

That now checks to ensure all $REQUIRED fields are present and non-empty, and prints the error message with all field names of incomplete submissions if not. Obviously this is only a demonstration, you should do whatever you feel is necessary for your application. You would probably also want to reshow the form (automatically completing all filled-in fields), and perhaps even use a template framework rather than echos all over the place.

 

 

That's pretty much 100% how I check for validation errors +1 :)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.