Hello, I am using very simple contact form with 3 fields (Name, Email, Message) at my website. It works almost fine. The problem is I would like to set all fields in this form as mandatory. Now only when the field Name is blank the message is not send but on the other hand when the field Name is filled (and other fields are blank) the message is send. And the second thing not that important but nice to have. I would like to preset form of email field (so for example if the sender forget to write @ in this field the message would not be send). This is php code I am using now: <? $email = "xx@gmail.com"; $site_uri = "http://www.xx.net/contact.html"; $site_name = "name"; $subject = $_POST['Name']; $msg = "Message from contact form\n"; $msg .= "Name: $_POST[name]\n"; $msg .= "Email: $_POST\n"; $msg .= "Message: $_POST[Message]\n"; if($subject == "") { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } elseif(mail($email,$subject,$msg)) { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } else { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } ?> I am beginner in php so if anybody knows how exactly should be the php code above modified I would appreciate it very much. Best regards.
Change this code: if($subject == "") { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } elseif(mail($email,$subject,$msg)) { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } else { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } PHP: To this: if (isset($_POST['Name']) && isset($_POST['Message']) && (strpos($_POST['email'])!==false)) { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } else { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } PHP: Generally it works by testing your variables and sending the user to the thank you page if they all validate. If one of them doesn't validate the user gets the error page.
Thank you very much for your fast answer. I have changed the code as you suggested but unfortunately when I fill all fields and try to send the message I still get to my error page and message is not send. Do you have any idea why that could be?
Hello, here is HTML: <form action="mailer.php" method="post"> <div align="left">Name: <br /> <input maxlength="80" name="meno" size="50" type="text" /><br /> Email: <br /> <input maxlength="80" name="email" size="50" type="text" /><br /> Message: <br> <textarea name="message" cols="40" rows="10"></textarea><br> <input type="submit" value="Submit" /><br> </div> </form> HTML: php: <? $email = "xx@xx.com"; $site_uri = "http://www.xx.net/contact.html"; $site_name = "name"; $subject = $_POST['name']; $msg = "Message from contact form\n"; $msg .= "Name: $_POST[name]\n"; $msg .= "Email: $_POST[email]\n"; $msg .= "Message: $_POST[message]\n"; if (isset($_POST['name']) && isset($_POST['message']) && (strpos($_POST['email'])!==false)) { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; } else { echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } ?> PHP:
try replacing this line if (isset($_POST['name']) && isset($_POST['message']) && (strpos($_POST['email'])!==false)) { PHP: with if (isset($_POST['name']) && isset($_POST['message']) && isset($_POST['email'])) { PHP:
3 things: You have a naming inconsistency between your html form and the script. The script wants 'name' you are sending it 'meno'. Second, the code I posted doesn't even try to mail your data, sorry. Third, I missed an argument in the strpos function. All of these can be fixed in the script, so there is no need to change your html form. Here is the correct code for the script: <? $email = "xx@xx.com"; $site_uri = "http://www.xx.net/contact.html"; $site_name = "name"; $subject = $_POST['meno']; $msg = "Message from contact form\n"; $msg .= "Name: $_POST[meno]\n"; $msg .= "Email: $_POST[email]\n"; $msg .= "Message: $_POST[message]\n"; if (isset($_POST['meno']) && isset($_POST['message']) && (strpos($_POST['email'], "@")!==false)) {if(mail($email,$subject,$msg)) echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_thank_you.html'>"; else echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } else {echo "<meta http-equiv='refresh' content='0;url=http://www.xx.net/contact_error.html'>"; } ?> PHP:
Thank you plog and the rest of the guys for your great effort. Now my contact form works exactly as it should. Thanks you very much again. Best regards. EDIT: Sorry for bothering again but I just realized that I am still facing one problem. The message is now send, but it is send also if I keep Name file blank. Did I just something mistyped or? Here is php code I am using: <? $email = "xx@xx.com"; $site_uri = "http://www.xx.net/contact2.html"; $site_name = "name"; $subject = $_POST['meno']; $msg = "Message from contact form\n"; $msg .= "Meno: $_POST[meno]\n"; $msg .= "Email: $_POST