Claire
Claire

Reputation: 1

PHP form submits successfully, but does not redirect

I've gone through and read a bunch of other questions asked about this issue, but I'm still as confused now as when I started with this problem. From what I have read, I've learned the following:

I don't believe any html is being done before the header() - But I'm new at this still so I'm possibly wrong. I haven't found any syntax errors. The form submits ok and I receive test emails.

What happens is, I fill out the form completely with valid text and hit Submit. The data is submitted and sent to my email and the page refreshes back to a fresh contact form (contact.php) where it should be instead going to my thanks.php page.

Here is my code, email addresses changed. Thank you in advance for any/all help! Much appreciated.

<?php 
$errors = array();
$missing = array();
if (isset($_POST['send'])) {
    $to = 'My Name <[email protected]>';
    $subject = 'Feedback from Contact Form';
    $expected = array('name', 'email', 'comments');
    $required = array('name', 'email', 'comments');
    $headers = "From: [email protected]\r\n";
    $headers .= "Content-Type: text/plain; charset=utf-8";
    $authenticate = null;
    if ($mailSent) {
        header('Location: thanks.php');
        exit();
    }
}
include './navigation.php'; 
?>


<?php
//mail process **Don't Touch**
$suspect = false;
$pattern = '/Content-Type:|Bcc:|CC:/i';

function isSuspect ($val, $pattern, &$suspect) {
    if (is_array($val)) {
        foreach ($val as $item) {
            isSuspect($item, $pattern, $suspect);
        }
    } else {
        if (preg_match($pattern, $val)) {
            $suspect = true;
        }
    }
}

isSuspect($_POST, $pattern, $suspect);

if (!$suspect) {
    foreach ($_POST as $key => $value) {
        $temp = is_array($value) ?  $value : trim($value);
        if (empty($temp) && in_array($key, $required)) {
            $missing[] = $key;
            $$key = '';
        } elseif(in_array($key, $expected)) {
            $$key = $temp;
        }
    }
}

if (!$suspect && !empty($email)) {
    $validemail = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    if ($validemail) {
        $headers .= "\r\nReply-to: $validemail";
    } else {
        $errors['email'] = true;
    }
}

if (!$suspect && !$missing && !$errors) {
    $message = '';
    foreach ($expected as $item) {
        if (isset($$item) && !empty($$item)) {
            $val = $$item;
        } else {
            $val = 'Not selected';
        }
        if (is_array($val)) {
            $val = implode(', ', $val);
        }
        $item = str_replace(array('_', '-'), ' ', $item);
        $message .= ucfirst($item) . ": $val\r\n\r\n";
    }
    $message = wordwrap($message, 70);

    $mailSent = mail($to, $subject, $message, $headers, $authenticate);
    if (!$mailSent) {
        $errors['mailfail'] = true;
    }
}
//end mail process
?>

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title>Contact Carla &#60;3</title>
    <link href="/design.css" rel="stylesheet" type="text/css"/>
</head>

<body id="contact">
<div id="main">
    <?php if (($_POST && $suspect) || ($_POST && isset($errors['mailfail']))) { ?>
    <span class="warning">Sorry your mail could not be sent.</span>
    <?php } elseif ($errors || $missing) { ?>
    <span class="warning">Please fix the item(s) indicated.</span>
    <?php } ?>

    <form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        <p>
            <label for="name">Name:
            <?php if ($missing && in_array('name', $missing)) { ?>
            <span class="warning">Who am I responding to?</span>
            <?php } ?>
            </label>
            <br>
            <input type="text" name="name" id="name"
            <?php
            if ($errors || $missing) {
                echo 'value="' . htmlentities($name, ENT_COMPAT, 'utf-8') . '"';
            }
            ?>
            >
        </p>

        <p>
            <label for="email">Email:
            <?php if ($missing && in_array('email', $missing)) { ?>
            <span class="warning">How will I respond to you?</span>
            <?php } elseif (isset($errors['email'])) { ?>
            <span class="warning">Invalid email address</span>
            <?php } ?>
            </label>
            <br>
            <input type="text" name="email" id="email"
            <?php
            if ($errors || $missing) {
                echo 'value="' . htmlentities($email, ENT_COMPAT, 'utf-8') . '"';
            }
            ?>
            >
        </p>

        <p>
            <label for="comments">Comments:
            <?php if ($missing && in_array('comments', $missing)) { ?>
            <span class="warning">Please say something..</span>
            <?php } ?>
            </label>
            <br>
            <textarea rows="7" cols="70" name="comments" id="comments"><?php 
            if ($errors || $missing) {
                echo htmlentities($comments, ENT_COMPAT, 'utf-8');
            }
            ?></textarea>
        </p>

        <p>
            <input type="submit" name="send" id="send" value="Send Comments">
        </p>
    </form>
</div>
</body>
<?php include './footer.php'; ?>
</html>

Upvotes: 0

Views: 55

Answers (1)

n099y
n099y

Reputation: 414

Your problem is this line

if ($mailSent) {
    header('Location: thanks.php');
    exit();
}

You have not set $mailsent anywhere above that statement. So it is never getting to that point.

Once it has passed that part of your code it will not pop back up to check if mail sent unless you call a function or similar down lower that points to it.

I hope that sets you on the right path, let me know if you need further help.

Upvotes: 2

Related Questions