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
https://forums.phpfreaks.com/topic/159428-self-submitting-form-and-posting-errors/
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!

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.