Jump to content

[SOLVED] Custom Error Array (not to replace PHP error handling)


Recommended Posts

Hello all!

 

I was just curious how one would go about making a custom error array for *application* error reporting.

 

In other words, I don't want this to replace PHP error handling, but rather inform me and the user of the script about any errors that occurred. I assume this is fairly easy and I am probably over thinking this. Here's what I came up with...

 

 

<?php

if ($result == "error"){
     $error[] = "There was an Error";
}

if ($error){
   foreach($error as $error){
      echo $error;
   }
}

?>

 

Is this the general concept?

 

Anything you would add/remove?

 

Thanks :D

rlelek

 

I posted this the other day in another thread

 

chronister,

 

look into that book i talked about earlier 'learning php 5' his method is really nice, makes functions for form processing... simple validation lines and all sorts of goodies.

 

heres the main page logic for all my form pages, just put this at the top and then build all the functions

 

<?php
if($_POST['_submit_check']){ //$_POST['_submit_check'] is a hidden field, makes redisplay possible
if($form_errors = validate_form()){
	show_form($form_errors);
} else {
	process_form();
}
} else {
show_form();
}

function show_form($errors = '') {

// make the form here
//start with
if ($errors){
print 'Please correct these errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
}

function validate_form(){
//contains simple validation like
//check that username is entered
if (trim(strlen($username)) == 0) {
$errors[]= "You must enter a username.";
}
}

function process_form(){
//what do you want to do with the entered data, whatever it is, do it here
}
?>

 

 

that make sense?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.