php - More professional error handling -



php - More professional error handling -

i have contact form , handle errors checking each field one-by-one "if" statement. find hard , can't seem find better/more productive way them working. heading saying "error" if 1 (or more) true. cant them work separate "if" statements.

here code:

$name = $_post['name']; //get info form $email = $_post['email'];//get info form $message = $_post['message'];//get info form if($name == ""){ echo"<p class='error'>please come in name.</p>"; } if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$email)){ echo "<p class='error'>your email address not valid.</p>"; } if($message == ""){ echo"<p class='error'>please come in message.</p>"; } else{ echo"all ok, send email code..."; }

edit: these errors validation of form.

but cant them work separate "if" statements.

just store error in variable

$error = array(); if($name == ""){ $error[] = "please come in name."; } if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$",$email)){ $error[] = "your email address not valid."; } if($message == ""){ $error[] = "please come in message."; } if (!$error) { // not echo here // send email // , utilize header("location:") redirect user page } else { echo "error"; foreach ($error $line) { echo "<p class='error'>$line</p>"; } }

php email error-handling

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -