1

I am working on the contact form for my Music Project's website.

The contact form seems to be working ok. If i input every field it sends the email (i get it ok on my gmail account) and if i don't input every field it gives error messages. But the strange thing is that after i hit send i get a blank page (address: MySiteRoot/contact.php )and it doesn't redirect. If i then click on the browsers "Go Back" button, i get the correct "error" messages, either the error or the message sent.

Why isn't is redirecting? Any ideas?

I have tried adding

exit();

after the

header('Location: /index.php');

but it didn't make any change.

I have both the index.php and the contact php on my site's root folder.

here is the code of my CONTACT.PHP

<?php

session_start();

require_once 'libs/phpmailer/PHPMailerAutoload.php';

$errors = [];

if(isset($_POST['name'], $_POST['email'], $_POST['message'])) {

    $fields = [
        'name' => $_POST['name'],
        'email' => $_POST['email'],
        'message' => $_POST['message']
    ];  

    foreach($fields as $field => $data) {
        if(empty($data)) {
            $errors[] = 'The ' . '<b>' . $field . '</b>' . ' field is required.';
        }
    }

    if(empty($errors)) {
        $m = new PHPMailer;

        $m -> isSMTP();
        $m -> SMTPAuth = true;

        /*$m -> SMTPDebug = 1;*/   

        $m -> Host = 'smtp.gmail.com';
        $m -> Username = '';  /* TOOK THEM OUT HERE ON THE POST ONLY */
        $m -> Password = '';  /* TOOK THEM OUT HERE ON THE POST ONLY */
        $m -> SMTPSecure = 'ssl';
        $m -> SMTPKeepAlive = true;
        $m -> Port = 465;

        $m -> isHTML(true);

        $m -> Subject = '4Elements Message';
        $m -> Body = 'From: ' . $fields['name'] . ' (' . $fields['email'] . ')<p>' . $fields['message'] . '</p>';

        $m -> FromName = '4Elements Contact';

        $m -> AddAddress('anatis@gmail.com', 'Anatis');

        if($m -> Send()) {
            $errors[] = 'Thanks! Your message was sent!';
            header('Location: /index.php');
        } else {
            $errors[] = 'Sorry, could not send email. Please try again later.';
        }
    } 

} else {
    $errors[] = 'Something went wrong.';
}

$_SESSION['errors'] = $errors;
$_SESSION['fields'] = $fields;

header('Location: /index.php');

on my INDEX.PHP i have at the beginning:

<?php

session_start();

require_once 'security.php';

$errors = isset($_SESSION['errors']) ? $_SESSION['errors'] : [];
$fields = isset($_SESSION['fields']) ? $_SESSION['fields'] : [];

?>

<!DOCTYPE HTML>
... THEN COMES SOME HTML CONTENT

Later on comes the FORM:

<?php if(!empty($errors)): ?>
    <div class="panel">
        <ul><li><?php echo implode('</li><li>', $errors); ?></li></ul>
    </div> <!-- end of .panel -->
<?php endif; ?>

<form action="contact.php" method="post">
    <label>
        <input type="text" name="name" autocomplete="off" placeholder="Name" <?php echo isset($fields['name']) ? ' value="' . e($fields['name']) . '"' : ''?>>
    </label>

    <label>
        <input type="email" name="email" autocomplete="off" placeholder="Email" <?php echo isset($fields['email']) ? ' value="' . e($fields['email']) . '"' : ''?>>
    </label>

    <label>
        <textarea name="message" rows="10" placeholder="Message"><?php echo isset($fields['message']) ? e($fields['message']) : ''?></textarea>
    </label>

    <input id="submitbutton" type="submit" value="send">

    </form>

and then at the end of the index.php i still have:

<?php
    unset($_SESSION['errors']);
    unset($_SESSION['fields']);
?>

i am working on my local server, with MAMP running PHP 5.6.2

Any ideas on what is going on? Thanks!

LuisBento
  • 678
  • 9
  • 16

2 Answers2

2

try to remove the header('Location: /index.php'); inside the m->Send() and let the only one at the end of the script. Even replace $errors = [] with $errors = Array() at the begin.

  • maybe you have some errors and the script terminate before the end. use error_reporting(E_ALL); to show the errors – Giuseppe Ferrara Mar 18 '15 at 13:04
  • should i add error_reporting(E_ALL); to the end of the contact.php file? – LuisBento Mar 18 '15 at 13:05
  • you are right, there is already something sent. i got the messages: Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /Applications/MAMP/htdocs/4Elements_V3/contact.php:12) in /Applications/MAMP/htdocs/4Elements_V3/contact.php on line 16 Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/4Elements_V3/contact.php:12) in /Applications/MAMP/htdocs/4Elements_V3/contact.php on line 75 – LuisBento Mar 18 '15 at 13:13
  • i have deleted a comment i had at the beggining of the contact.php file and now it works! Thanks! =) – LuisBento Mar 18 '15 at 13:18
1

Have you tried with using javascript instead of header('Location: /index.php');

echo '<script>window.location.href="/index.php";</script>'

Also, have you checked any warning or error log file. If you are getting this type of error message "Cannot modify header information - headers already sent" then try this link How to fix "Headers already sent" error in PHP

I hope it should work for you.

Community
  • 1
  • 1