TheFilmGod Posted May 24, 2009 Share Posted May 24, 2009 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. Quote Link to comment Share on other sites More sharing options...
Cosizzle Posted May 24, 2009 Share Posted May 24, 2009 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! Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted May 24, 2009 Share Posted May 24, 2009 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.