morocco-iceberg Posted March 14, 2010 Share Posted March 14, 2010 I'm having difficulties getting my form to work properly, it seems to ignore the first 'if' section even if the fields have nothing in them and skips to the email validation. Help? Alos it would be cool if someone could help me with making it spam proof? Thanks.. <?php $subject = $_POST['subject']; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $what = $_POST['what']; $re = $subject; $to = "info@klds.com.au"; $msg = "$message \r\n \r\n $name, \r\n $email"; if (!$subject||!$name||!$email||!$message||!$what){ echo "Error: All fields must be completed!"; }else if($what!="Green"||$what!="green"){ echo "You got the question wrong! Please try again."; }else{ $email = trim($email); $_ename = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_'{|}~]+"; $_host = "([0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if (!preg_match($_ename."@".$_host.$_tlds,$email)){ echo "E-mail address is not valid. Please enter a valid e-mail address."; }else{ mail($to, $re, $msg); echo "Thank-you for your enquiry, your message was succesfully sent. We'll get back to you as soon as possible."; } } ?> Quote Link to comment Share on other sites More sharing options...
codebyren Posted March 14, 2010 Share Posted March 14, 2010 I can't immediately see why it wouldn't be working. You might have spaces in some of the fields which would then throw the ( ! $variable) check because it does actually contain something (spaces). You could try something (which I haven't tested here and now) like: // Function to fetch values in $_POST array. // Returns NULL if value doesn't exist (wasn't submitted) or was only spaces. function get_post($post_value) { if (isset($_POST["$post_value"] AND strlen(trim($_POST["$post_value"])) != 0)) { return trim($_POST["$post_value"]); } else { return NULL; } } $subject = get_post('subject'); $name = get_post('name'); // etc... if ( ! $subject OR ! $name) { // etc... echo "bad..."; } This is pretty shallow in terms of validation checks but it might be fine. As for spam... that's a bit more complicated. There are lots of PHP classes/libraries etc. that help with this: http://recaptcha.net/ http://www.phpcaptcha.org/ etc. Some sites will get by with something as simple as a hidden field that shouldn't be filled in (because it's hidden). Spam bots will generally fill this out automatically - so you can assume that it's spam if it's filled out... Not a 100% solution but it works for a lot of basic sites. It obviously won't deter human spammers though. The spam question is pretty big... Hope this helps. Quote Link to comment Share on other sites More sharing options...
morocco-iceberg Posted March 14, 2010 Author Share Posted March 14, 2010 Thanks! it was the spaces in the form fields that were doing it. And thanks for the links. 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.