Hiya, I made this script years ago, and now don't understand what is happening. I wondered if someone would be so kind as to recode it using proper variable names etc instead of just using names like $k and $p (I know these names are bad, and I no longer do this. But like I said it was a long time ago since I coded this ). Also, how do you output the appropriate error messages next to the correct field within the form? Is there a better way of doing this? <?php $post = array("name" => "", "email" => "", "phone" => ""); $errors = $post; if (isset($_POST["submit"])) { foreach ($post as $k => $v) $post[$k] = stripslashes($_POST[$k]); if ($post["name"] == "") $errors["name"] = "error"; if (!preg_match('/[^@]+@[^.]+(\.[^.]+)+/', $post["email"])) $errors["email"] = "error"; if ($post["phone"] == "") $errors["phone"] = "error"; $error = false; foreach($errors as $k => $v) { if ($errors[$k] != "") $error = true; } if (!$error) { $message = ""; foreach($post as $k => $v) if ($v != "") $message .= "$k: $v\n"; $sent = mail("info@company.com", "Subject", $message, "From: info@company.com"); } } ?> <form name="form" method="post" action=""> SECTION #1<br> <br> Name: <input name="name" type="text" id="name"> <br> <br> E-Mail: <input name="email" type="text" id="email"> <br> <br> Phone: <input name="phone" type="text" id="phone"> <br> <br> <input name="send" type="submit" id="send" value="Send"> <input name="reset" type="reset" id="reset" value="Reset"> </form> Code (markup):
All you need to do is search and replace, $k to $key and $v to $value. The $key ($k) would be name, email, phone The $value ($v) would be whatever was submitted in each. As for the javascript suggestion, its great for front line processing but you still need the validation in PHP or you have no security.
Thanks Boatsource! - I agree. JavaScript alone is a bad idea due to JavaScript being able to be disabled! Is there a better way this can be coded in PHP, also how do I display the error message stored in $errors['fieldname'] variables next to the appropriate fields? Also, what is actually happening in the script? I can't remember. Without being too cheeky, would it be possible for you to comment it? I just wish I could remember!