richard_PHP Posted March 22, 2010 Share Posted March 22, 2010 Hi all, I have a contact form on my website that I need help with. Fill out the form as usual, click submit and it sends an email with the info. Yes. But what happens should a necessary field get left out? Well then the email won't send yes? NO! I've worked on the server-side page for the sendmail but am now at a loss and am wondering where I've gone wrong. Basically there are 5 fields (name*, email*, business name, number* and message* (*=required)), got it HALF working, if one required field is entered then it sends the email. I want it if any field is left out then it says an error. Here's the code: $name = $_POST['name']; $email = $_POST['email']; $business = $_POST['business']; $number = $_POST['number']; $message = $_POST['message']; //leave the next 2 lines alone $headers .= "MIME-Version: 1.0rn"; $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; if ($name . $email . $number . $message == "") { echo "<p>You appear to not have filled in the necessary fields.</p>"; echo "<p>Please click <a href='contact.html'>here</a> to re-enter your information.</p>"; } Is there also a way to send the user back to the form (.html page) with all their information still entered? Many thanks! Quote Link to comment https://forums.phpfreaks.com/topic/196169-send-mail-help/ Share on other sites More sharing options...
schilly Posted March 22, 2010 Share Posted March 22, 2010 you want this in your if statement. if any of those variables are empty ( "", NULL, 0 or false) it will result in true and trigger your error message. if (empty($name) || empty($email) || empty($number) || empty($message)) { Is there also a way to send the user back to the form (.html page) with all their information still entered? If you want the POST data still in the form you will either need to POST the form to the same page or put the data in a session then use header to redirect back to the form. Easiest is to POST to the same page then do value="<?php echo $_POST['business']; ?>" in your text fields, etc. Then you can put your error message in a variable and display that in the page as well. Quote Link to comment https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030161 Share on other sites More sharing options...
Psycho Posted March 22, 2010 Share Posted March 22, 2010 Here is a rough example of how you could build the Form page and the Processing page in one. This allows you to redisplay the form (with the submitted values) when there is a validation error. <?php $name = trim($_POST['name']); $email = trim($_POST['email']); $business = trim($_POST['business']); $number = trim($_POST['number']); $message = trim($_POST['message']); $error = ''; if (isset($_POST['name']) && (empty($name) || empty($email) || empty($number) || empty($message))) { $error .= "<p>You appear to not have filled in the necessary fields.</p>"; } else { //validation passed, create and send the email $headers .= "MIME-Version: 1.0rn"; $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; mail($to, $subject, $message, $headers); //redirect to success page header('Location: http://www.example.com/message_sent.php') exit(); } ?> <html> <body> <?php echo $error; ?> <form action=""> *Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br> *Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br> Business: <input type="text" name="business" value="<?php echo $business; ?>" /><br> *Number: <input type="text" name="number" value="<?php echo $number; ?>" /><br> *Message:<br /><textarea name="message"><?php echo $message; ?></textarea><br> <button type="submit">Send Email</button> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030165 Share on other sites More sharing options...
richard_PHP Posted March 22, 2010 Author Share Posted March 22, 2010 Thanks for the help. 'schilly', your code worked fine. 'mjdamato'...... I've tried your code and it has come up with: Warning: Cannot modify header information - headers already sent by (output started at ****htdocs/contact.php:7) in ******htdocs/contact.php on line 45. Any help? Line 45 contains: header('Location: mail.php'); Quote Link to comment https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030180 Share on other sites More sharing options...
richard_PHP Posted March 22, 2010 Author Share Posted March 22, 2010 Okay, after changing the code from the shockingly bad bit code having two opening <html> tags ( ), I now have the error message: Warning: Cannot modify header information - headers already sent by (output started at /*********/htdocs/contact.php:7) in /********/htdocs/contact.php on line 45 Here's the code: <?php $name = trim($_POST['name']); $email = trim($_POST['email']); $business = trim($_POST['business']); $number = trim($_POST['number']); $message = trim($_POST['message']); $error = ''; if (isset($_POST['name']) && (empty($name) || empty($email) || empty($number) || empty($message))) { $error .= "<p>You appear to not have filled in the necessary fields.</p>"; } else { //validation passed, create and send the email $headers .= "MIME-Version: 1.0rn"; $headers .= "Content-Type: text/html; charset=ISO-8859-1rn"; mail($to, $subject, $message, $headers); //redirect to success page header('Location: mail.php'); exit(); } ?> <?php echo $error; ?> <form action=""> *Name: <input type="text" name="name" value="<?php echo $name; ?>" /><br> *Email: <input type="text" name="email" value="<?php echo $email; ?>" /><br> Business: <input type="text" name="business" value="<?php echo $business; ?>" /><br> *Number: <input type="text" name="number" value="<?php echo $number; ?>" /><br> *Message:<br /><textarea name="message"><?php echo $message; ?></textarea><br> <button type="submit">Send Email</button> </form> Quote Link to comment https://forums.phpfreaks.com/topic/196169-send-mail-help/#findComment-1030201 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.