0

My form is working, but when the form is submitted, then I receive Warning: Cannot modify header information - headers already sent by.

I have tried everything i can possibly think of like after the header('location: ../thanks', true, 303); i put exit;

And it still does work. I know this has probably been asked before, but i have tried most of the methods on this site and I have even attempted to google for a solution, but again nothing. I know the php code works as I have use the file before on a previous project.

HTML Code:

<form method="post" action="includes/contact-form-process.php">
                            <div>
                                <span><label>Name</label></span>
                                <span><input name="name" type="text" class="textbox" required="required"></span>
                            </div>
                            <div>
                                <span><label>E-mail</label></span>
                                <span><input name="email" type="text" class="textbox" required="required"></span>
                            </div>
                            <div>
                                <span><label>Subject</label></span>
                                <select name="reason" required="required">
                                    <option value="">---</option>
                                    <option value="Book A Bike Build Appointment">Book A Bike Build Appointment</option>
                                    <option value="Get A Quote">Get A Quote</option>
                                    <option value="General Inquiry">General Inquiry</option>
                                    <option value="Feedback">Feedback</option>
                                    <option value="Complaint">Complaint</option>
                                </select>
                            </div>
                            <div>
                                <span><label>Message</label></span>
                                <span><textarea name="message" required="required"> </textarea></span>
                            </div>
                           <div>
                                <span><input type="submit" name="submit" value="Submit"></span>
                          </div>
                        </form>

PHP Code

<?php
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

if(isset($_POST["submit"])) //If the form is submitted
{
$notification="";  //Used for catching all your messages
//Retrieve form data. 
//GET - user submitted data using AJAX
//POST - in case user does not support javascript, we'll use POST instead
$errors = [];

$name = $_POST['name'];
$email = $_POST['email'];
$reason = $_POST['reason'];
$message = $_POST['message'];

//flag to indicate which method it uses. If POST set it to 1
if ($_POST) $post=1;
//Simple server side validation for POST data, of course, 
//you should validate the email
if (!$name) $errors[count($errors)] = 'Please enter your name.';
if (!$email) $errors[count($errors)] = 'Please enter your email address.'; 
if (!$reason) $errors[count($errors)] = 'Please tell us why your contacting us.'; 
if (!$message) $errors[count($errors)] = 'Please tell us more informaton on why your contacting us.'; 
//if the errors array is empty, send the mail
if (!$errors) {
//recipient - change this to your name and email
$to = 'email address here';    
//sender
$from = $email;

//subject and the html message
$subject = 'Hello from ' . $name .
$message = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<p>You have recieved a message from ' . $name . ' using your contact form at www.example.co.uk/contact
<table>
    <tr><td>Name: </td><td>' . $name . '</td></tr>
    <tr><td>Email Address: </td><td>' . $email . '</td></tr>
    <tr><td>What is the reason for contacting us: </td><td>' . $reason . '</td></tr>
    <tr><td>Message: </td><td>' . $message . '</td></tr>
</table>
</body>
</html>';
//send the mail
$result = mail($to, $subject, $message, $from);

//if POST was used, display the message straight away
if ($_POST) {
    if ($result) header('location: ../thanks', true, 303);
    else $notification.= 'Sorry, unexpected error. Please try again later';

//else if GET was used, return the boolean value so that 
//ajax script can react accordingly
//1 means success, 0 means failed
} else {
    $notification.= $result;    
}
//if the errors array has values
} else {
//display the errors message
for ($i=0; $i<count($errors); $i++) echo $errors[$i] . '<br/>';
$notification.= '<a href="../contact">Back</a>';
exit;
}
//Simple mail function with HTML header
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    $headers .= 'From: ' . $from . "\r\n";
    $result = mail($to,$subject,$message,$headers);
    if ($result) return 1;
    else return 0;
}
} //First If loop
?>

Any help would much and greatly appreciated and thank you in advance.

0 Answers0