Im new to Php so please be gentle.
I have written a website and i have added a subscribe to newsletter section. The user adds their name and email address before pressing submit. I get the email so the most part is working but the form doesnt return me to the main page of the website. Can anyone point out where i am going wrong please?
Thanks in advance
The code on the website is:
<form name="frmcontact" id="frmcontact" action="php/contact.php" method="post">
<input id="name" name="name" type="text" value="Name"
onblur="this.value=(this.value=='') ? 'Name' : this.value;" onfocus="this.value=(this.value=='Name') ? '' : this.value;" />
<input id="email" name="email" type="text" value="Email"
onblur="this.value=(this.value=='') ? 'Email' : this.value;" onfocus="this.value=(this.value=='Email') ? '' : this.value;" />
<input name="submit" id="send" type="submit" value="Message" />
</form>
The code in contact.php is:
<?php session_start();
if(!$_POST) exit;
$address = 'email@emailaddress.com';
$email_subject ="Add me to the mailing list";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "Please add me to the mailing list ";
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$msg = "You have been contacted by $name with regards to $subject.
";
$msg .= "$message
";
$msg .= "You can contact $name via email, $email.
";
$msg .= "-------------------------------------------------------------------------------------------
";
if(@mail($address, $subject, $msg, "From: $email
Return-Path: $email
"))
{echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
else
{echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
header('Location:http://www.websiteaddress/homepage.html');
?>
Remove All output before header
use exit
after header
and also consider to give space marked below
v
header('Location: http://www.websiteaddress/homepage.html');
exit;
use correct directory path
if(isset($_POST['submit'])){
$address = 'email@emailaddress.com';
$email_subject ="Add me to the mailing list";
$email = $_REQUEST['email'];
$name = $_REQUEST['name'];
$subject = "Please add me to the mailing list ";
if(get_magic_quotes_gpc()) { $comment = stripslashes($comment); }
$e_subject = 'You\'ve been contacted by ' . $name . '.';
$msg = "You have been contacted by $name with regards to $subject.
";
$msg .= "$message
";
$msg .= "You can contact $name via email, $email.
";
$msg .= "-------------------------------------------------------------------------------------------
";
if(@mail($address, $subject, $msg, "From: $email
Return-Path: $email
"))
{echo "<p class='ajax_success'>Thanks for Contact Us.</p>"; }
else
{echo "<p class='ajax_failure'>Sorry, Try again Later.</p>"; }
header('Location: homepage.html');
}
you cant output anything before using the header()
HTTP-Headers should be sent before any output from the server. If you have an output before, the server will output a warning like 'Headers already been sent'
<?php
ob_start();
echo "redirecting now ....";
header('Location: http://www.websiteaddress/homepage.html');
exit();
ob_end_flush();
?>