foevah Posted September 13, 2007 Share Posted September 13, 2007 Hi everyone I was just wondering how to make my contact form recognize if the user has actually entered any information into the fields. Right now if the user just clicks submit then an email will be sent to me without them having to enter any info into the fields.. Please can someone tell me how to prevent this? I would also like to know how I can filter the message field from letting the user enter in profanity? This is the HTML for my contact form: <form method="post" action="contact.php"><p class="class2"> your name:<br /> <input type="text" name="name" size="19"><br />your email:<br /> <input type="text" name="email" size="19"><br />subject:<br /> <textarea rows="5" name="message" cols="30"></textarea> <br /> <input type="submit" value="submit" name="submit"> </form> This is the PHP for my contact form: <?php if(isset($_POST['submit'])) { $to = "jecgard@gmail.com"; $subject = "Form Tutorial"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message"; echo "Data has been submitted to $to!"; mail($to, $subject, $body); } else { } ?> Quote Link to comment Share on other sites More sharing options...
Jessica Posted September 13, 2007 Share Posted September 13, 2007 you need to check if those $_POST variables are set, what the strlen of each is, etc. If they are all what you want, send the mail otherwise go back. What is the question? Quote Link to comment Share on other sites More sharing options...
foevah Posted September 13, 2007 Author Share Posted September 13, 2007 my question is: I want to make my contact form recognize if the user hasn't enter in all the fields. I want my form to say next to the fields that haven't got the correct info in them to say something like (after submission) "you have forgotten to enter your name.." or if they use a swear word in the message field I want it to say "no swearing allowed!" any ideas? can you show me some example code please Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 13, 2007 Share Posted September 13, 2007 here is a simple error i use on mine. <?php if($_POST[variable]==""){ echo $error[variable]="Please post a variable here"; } else {unset ($error[variable]);} if ($error){ ?> HTML CODE <?php } else { more php code } ?> then if you want to echo out the errors if the form comes up empty find the input for the post variable and after it put <?php echo $error[variable]; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted September 13, 2007 Share Posted September 13, 2007 <?php $swarewords= array(bad,mouth,mother,screw,off); if ($swarewords=!) { echo "no sware words were found."; } else { echo "You naughy person you!"; } ?> 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.