Jump to content

Recommended Posts

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 "!";

} 
Link to comment
https://forums.phpfreaks.com/topic/182022-php-form-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/182022-php-form-help/#findComment-960169
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.