leet8845 Posted August 13, 2007 Share Posted August 13, 2007 Hi there, I sometimes get blank emails from contact form on my site. It happens when users click submit by mistake. My exsiting code doesnt check the fields have any content before sending the form. I need to add some code to check that fields have been filled out on my form. Does anyone know if its possible to add anything to exsiting code below to do this. I'm mainly bothered that the fields just have some text, nothing more complex really. Here's my code: <?php // Getformdataandcreateemail $Email2="mail@mysite.co.uk"; $email=$_POST['Email']; $name=stripslashes($_POST['Name']); $subject=stripslashes($_POST['Subject']); $messagecont=stripslashes($_POST['Message']); $message= <<<EOD -------------------------------- Enquiry from your website -------------------------------- Name: $name Subject: $subject EmailAddress: $email Message: $messagecont -------------------------------- End of Message -------------------------------- EOD; //Sendemail @mail($Email2,$subject,$message, "From:$Email2"); header("Location:thankyou.html"); ?> any help truly appreciated Quote Link to comment Share on other sites More sharing options...
trq Posted August 13, 2007 Share Posted August 13, 2007 Does anyone know if its possible to add anything to exsiting code below to do this. Of course it possible. Just use conditionals to weed out empty fields. Quote Link to comment Share on other sites More sharing options...
Nakor Posted August 13, 2007 Share Posted August 13, 2007 <?php // Check email is not empty if (! empty ($_POST['Email']) ) { // Getformdataandcreateemail $Email2="mail@mysite.co.uk"; $email=$_POST['Email']; $name=stripslashes($_POST['Name']); $subject=stripslashes($_POST['Subject']); $messagecont=stripslashes($_POST['Message']); $message= <<<EOD -------------------------------- Enquiry from your website -------------------------------- Name: $name Subject: $subject EmailAddress: $email Message: $messagecont -------------------------------- End of Message -------------------------------- EOD; //Sendemail @mail($Email2,$subject,$message, "From:$Email2"); header("Location:thankyou.html"); } else { // No email was sent header("Location:form.html"); } ?> Quote Link to comment Share on other sites More sharing options...
HaLo2FrEeEk Posted August 13, 2007 Share Posted August 13, 2007 // Getformdataandcreateemail $Email2 = "mail@mysite.co.uk"; $email = (isset($_POST['Email'])) ? $_POST['Email'] : die("Please enter a valid Email address"); $name = (isset($_POST['Name'])) ? stripslashes($_POST['Name']) : die("Please enter a \"From\" name"); $subject = (isset($_POST['Subject'])) ? stripslashes($_POST['Subject']) : die("Please enter a Subject for your message"); $messagecont = (isset($_POST['Message'])) ? stripslashes($_POST['Message']) : die("Please enter a message in the textbox"); Should help you out there. Quote Link to comment Share on other sites More sharing options...
leet8845 Posted August 13, 2007 Author Share Posted August 13, 2007 Thanks for all your help. Nakor the code you gave me works great, I studied it and was able to add the same for the other fields. Thanks very very much 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.