Jump to content

Self submitting form and posting errors


TheFilmGod

Recommended Posts

How do you code a self submitting form that posts all errors (if there are any errors) in a neat way?

 

I know how to code such a form without a problem... I just don't know how to code it WELL/shorthand?

 

Do you put things into a function? Is there some advanced php template producer? Just wondering.

Link to comment
Share on other sites

I've always gone with an error array.... so put the errors into an array and check it later

 

$error = array();

if (isset($_POST['submit'])) {									// get the session that was posted
// 
if (empty($_POST['field1'])) {							
	$error[] = 'field one';			
} else {												
	$field1 = trim($_POST['field1']);						
}

// 
if (empty($_POST['field2'])) {							
	$error[] = 'field two';			
} else {												
	$field2 = trim($_POST['field2']);						
}

// 
if (empty($_POST['field3'])) {							
	$error[] = 'field three';			
} else {												
	$field3 = trim($_POST['field3']);						
}

if (empty($error)) {
	// put in DB or whatever...
}
else {
	foreach ($error as $errorMsgArr) {
		echo ('There was an error, check ' . $errorMsgArr . '<br />');
	}
}
}

 

Of course the foreach can be put into a function and that function can be called upon at any time. Also, I tend to build this around a class that will process all the data. This is how I tend to process my data, hope it works, or helps!

Link to comment
Share on other sites

Is there some advanced php template producer?

 

You could use any one of the many frameworks.  I'm most familiar with Zend (http://framework.zend.com) and I know that their form class(es) can do what you want.

 

They use an especially nice decorator format that makes things easy to change.  Padriac Brady wrote a nice tutorial about ZF a while back: http://blog.astrumfutura.com/archives/367-Example-Zend-Framework-Blog-Application-Tutorial-Parts-1-8-Revisited.html

Link to comment
Share on other sites

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.