James Dean Posted November 7, 2010 Share Posted November 7, 2010 First off, hello. This is my first pHp code, and still tying to get a grasp on it all. This is the pHp i have for my mailing form. Now i wish to make the user have to put in a valid email address. Except it isn't working for me. The following script isn't coming up with any errors, but it is still letting the email field be submitted with any text. <?php if ($_POST['submit']) { $name = $_POST['name']; $email = $_POST['email']; $comments = $_POST['comments']; $errorstring = ""; if (!$name) $errorstring = $errorstring."Name<br>"; if (!$email) $errorstring = $errorstring."email<br>"; if (!$comments) $errorstring = $errorstring."comments<br>"; if ($errorstring!="") echo "You missed the following:<br>$errorstring"; else { $webMaster = "name@site.com"; $emailSubject = "Contact Us Form"; $mail_from = "$email"; $name = $_POST['name']; $email = $_POST['email']; $comments = $_POST['comments']; function valid_email($email) { if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email)) { return TRUE; } else { return FALSE; } } if (!valid_email($email)) { die('bad email'); } $body = ' <br> Name ' . $name . ' <br><br> Email ' . $email . ' <br><hr><br> Comments<p> ' . $comments . ' '; $headers = "From: $email\r\n"; $headers .= "Content-type: text/html\r\n"; $success = mail($webMaster, $emailSubject, $body, $headers); $redirect = file_get_contents('redirect.html'); echo $redirect; } } ?> Thank you. Quote Link to comment Share on other sites More sharing options...
James Dean Posted November 7, 2010 Author Share Posted November 7, 2010 Anyone, please? Quote Link to comment Share on other sites More sharing options...
Anti-Moronic Posted November 8, 2010 Share Posted November 8, 2010 First, it's PHP, not pHp..or you can use the common php. Right - few issues: Don't do this: $errorstring = $errorstring."Name<br>"; Do this instead: $errorstring .= "Name<br>"; Same thing. Don't indent *everything* after your IF statement. Only indent what is within your if statement. if (!$name) $errorstring = $errorstring."Name<br>"; if (!$email) $errorstring = $errorstring."email<br>"; ..should be: if (!$name) $errorstring = $errorstring."Name<br>"; if (!$email) $errorstring = $errorstring."email<br>"; //etc. Your function is declared deep within your statements, that can't be good. (didn't even know it was possible). function valid_email($email) { if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email)) { return TRUE; } else { return FALSE; } That should be defined well before all of this right at the top. ..and your validation for email is: if(eregi("^[\w\-]+?\@\w+?\.\w+$", $email)) So if something is still validating when it should, it is down to that because you are correctly passing the email to the function and returning as you should. 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.