Cyberdave Posted November 18, 2009 Share Posted November 18, 2009 I have set up the following form to send mail from a website I am working on. I have set it to redirect to a thank you page if it sends and also an error page (which I have designed) if it fails. However, it isn't working with the code below. If I remove the if (empty... part it will send fine, but then I don't have error message if all the fields haven't been filled in. Here is the code: <?php if(isset($_POST['submit'])) { if (empty($_POST['name']) || empty($_POST['phone']) || empty($_POST['email']) || empty($_POST['message']) ){ header( "Location: form-error.php"); } } $to = "me@website.ie"; $subject = "Email from Website"; $name_field = $_POST['name']; $phone_field = $_POST['phone']; $email_field = $_POST['email']; $message = $_POST['message']; foreach($_POST['check'] as $value) { $check_msg .= "\n$value\n"; } $body = "Name: $name_field\n\n E-Mail: $email_field\n\n Phone: $phone_field\n\n Message:\n\n $message\n\n"; $header="From: $email_field\r\n"; mail($to, $subject, $body,$header); header("Location: thanks.php"); } else { echo "!"; } Quote Link to comment https://forums.phpfreaks.com/topic/182022-php-form-help/ Share on other sites More sharing options...
Cyberdave Posted November 18, 2009 Author Share Posted November 18, 2009 Just bumping this... Quote Link to comment https://forums.phpfreaks.com/topic/182022-php-form-help/#findComment-960097 Share on other sites More sharing options...
mikesta707 Posted November 18, 2009 Share Posted November 18, 2009 Can I see your form? Quote Link to comment https://forums.phpfreaks.com/topic/182022-php-form-help/#findComment-960112 Share on other sites More sharing options...
DavidAM Posted November 18, 2009 Share Posted November 18, 2009 You have an extra closing brace in the if (empty ... part, which closes the if(isset ... part and orphans the else (at the bottom). I don't see how that produces the results you indicate, but it does need to be fixed. <?php if(isset($_POST['submit'])) { if (empty($_POST['name']) || empty($_POST['phone']) || empty($_POST['email']) || empty($_POST['message']) ){ header( "Location: form-error.php"); } } // <--- This closes the "if(isset" above which will cause the form to send mail even if not posted You should turn on error reporting to see what other errors you might be getting. Place this inside the first openning tag (<?php) in every PHP file: error_reporting(E_ALL); also, add exit() after the header() call. That will prevent the remaining code from executing while the browser receives and processes the header you sent. Without it, it is possible the page might send the email after sending the header(). You should also check to see if $_POST['check'] exists before looping through it: $check_msg = ''; if (isset($_POST['check'])) { foreach($_POST['check'] as $value) { $check_msg .= "\n$value\n"; } } Post any error messages you get along with the updated code and we'll see where we are. Quote Link to comment https://forums.phpfreaks.com/topic/182022-php-form-help/#findComment-960169 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.