DarkxPunk Posted September 25, 2012 Share Posted September 25, 2012 Hey everyone, So I have been working on a cleaner contact form for my website, I have found a few tools online and tutorials to help out but I think the community here at W3Schools Forums may be able to even help me improve this, and maybe assist me in some questions. So first off the resource I used to build my contact form is here: http://www.html-form-guide.com/contact-form/php-email-contact-form.htmlIts a simply contact form and offered a nice JS filter which works like a charm. Here is my resulting contact form: <form action="contact.php" method="post" name="contact"> <label>Name: <span class="error" id="contact_name_errorloc"></span></label> <input id="contactName" type="text" name="name"> <label>E-Mail: <span class="error" id="contact_email_errorloc"></span></label> <input id="contactEmail" type="text" name="email"> <label>Subject: <span class="error" id="contact_subject_errorloc"></span></label> <input id="contactSubject" type="text" name="subject"> <label>Message: <span class="error" id="contact_message_errorloc"></span></label> <textarea id="contactMessage" name="message"></textarea> <input id="contactSend" type="submit" name="submit" value="Send"> </form> <script type="text/javascript"> var frmvalidator = new Validator("contact"); frmvalidator.EnableOnPageErrorDisplay(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please provide your name."); frmvalidator.addValidation("email","req","Please provide your email."); frmvalidator.addValidation("email","email","Please enter a valid email address."); frmvalidator.addValidation("subject","req","Please provide a subject."); frmvalidator.addValidation("message","req","Please write us a message."); </script> <?php $errors = ''; $myemail = 'example@example.com'; if( empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['message'])) { $errors .= "\n Error: all fields are required"; } $name = $_POST['name']; $email_address = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; } if( empty($errors)) { $to = $myemail; $email_subject = "ORMT: $subject"; $email_body = "Name: $name\n Email: $email_address\n Message\n $message"; $headers = "From: $myemail\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); echo "<p>Thank you for your interest, we will be in contact soon.</p>"; } ?> Now I am looking for any ideas on how to clean this up, as well as a way for (if JS is turned off) the PHP will display the errors. I tried after the 'if( empty($errors))' to put 'else {echo $errors;}' but it decides to write the errors even before I click send. Thanks for any help guys, and sorry if I ask for allot of explanations, I am still learning PHP Link to comment Share on other sites More sharing options...
Don E Posted September 25, 2012 Share Posted September 25, 2012 (edited) It is writing the errors because you need to have the PHP code execute/run after the form has been submitted. For example, just an example: <?php if(isset($_POST['submit'])) //this gets set when the submit button is clicked and the code below runs{ $errors = ''; $myemail = 'example@example.com'; $name = $_POST['name']; $email_address = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; if( empty($name) || empty($email_address) || empty($subject) || empty($message)) { $errors .= "\n Error: all fields are required"; } if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; } if(empty($errors)) { $to = $myemail; $email_subject = "ORMT: $subject"; $email_body = "Name: $name\n Email: $email_address\n Message\n $message"; $headers = "From: $myemail\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); echo "<p>Thank you for your interest, we will be in contact soon.</p>"; } } ?> Edited September 25, 2012 by Don E Link to comment Share on other sites More sharing options...
kanchatchai Posted September 25, 2012 Share Posted September 25, 2012 (edited) <?php $errors = ''; myemail = 'example@example.com';$name="";$email_address="";$subject="";$message="";if (isset($_POST['submit'])){ if( empty($_POST['name']) || empty($_POST['email']) || empty($_POST['subject']) || empty($_POST['message'])) { $errors .= "\n Error: all fields are required"; } if (isset($_POST['name'])) $name = $_POST['name']; ; if (isset($_POST['email'])) $email_address = $_POST['email']; if (isset($_POST['subject'])) $subject = $_POST['subject']; if (isset($_POST['message'])) $message = $_POST['message'];} if (!preg_match( "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email_address)) { $errors .= "\n Error: Invalid email address"; } if(empty($errors)) { $to = $myemail; $email_subject = "ORMT: $subject"; $email_body = "Name: $name\n Email: $email_address\n Message\n $message"; $headers = "From: $myemail\n"; $headers .= "Reply-To: $email_address"; mail($to,$email_subject,$email_body,$headers); echo "<p>Thank you for your interest, we will be in contact soon.</p>"; } ?> <form action="" method="post" name="contact"> <label>Name: <span class="error" id="contact_name_errorloc"></span></label> <input id="contactName" type="text" name="name" value="<?PHP echo $name; ?>"> <?PHP if (isset($_POST['submit'])&&$name=="") echo "Please provide your name."; ?> <br/> <label>E-Mail: <span class="error" id="contact_email_errorloc"></span></label> <input id="contactEmail" type="text" name="email" value="<?PHP echo $email_address; ?>"><?PHP if (isset($_POST['submit'])&&$email_address=="") echo "Please provide your email."; ?><br/> <label>Subject: <span class="error" id="contact_subject_errorloc"></span></label> <input id="contactSubject" type="text" name="subject" value="<?PHP echo $subject; ?>"><?PHP if (isset($_POST['submit'])&&$subject=="") echo "Please provide your subject."; ?><br/> <label>Message: <span class="error" id="contact_message_errorloc"></span></label> <textarea id="contactMessage" name="message" ><?PHP echo $message; ?></textarea><?PHP if (isset($_POST['submit'])&&$message=="") echo "Please provide your message."; ?> <input id="contactSend" type="submit" name="submit" value="Send"> </form> <?PHP echo $errors; ?> Edited September 25, 2012 by kanchatchai Link to comment Share on other sites More sharing options...
DarkxPunk Posted September 25, 2012 Author Share Posted September 25, 2012 Thank you both, its a little late now. For sure I will read deeper into these in the morning, but they seem very good so far. Will update y'all with my end results Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now