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!