php form ... please help!

please help!
there is a feedback form i need to make work but it is in php and i have never used php before

http://www.keystonecoaching.co.za/test/contact_php.htm

(its a generic form i found but i cant make it work as you will see the error i receive is

Warning: Cannot modify header information - headers already sent by (output started at /home/keystone/public_html/test/contact.php:3) in /home/keystone/public_html/test/contact.php on line 31

i was told that this is because i am putting the php script on the .php file below the head section. so i moved it but still nothing … where must this php script be placed??

here is a copy of the .php page … im not sure what else you need
i would really appreciate the help … do i perhaps just delete all the html stuff and only have the .php script?


<?php $to = $_REQUEST['sendto'] ; $from = $_REQUEST['Email'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data";

$fields = array();
$fields{“Name”} = “Name”;
$fields{“Company”} = “Company”;
$fields{“Email”} = “Email”;
$fields{“Phone”} = “Phone”;
$fields{“list”} = “Mailing List”;
$fields{“Message”} = “Message”;

$body = “We have received the following information:\n\n”; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }

$headers2 = “From: [email protected]”;
$subject2 = “Thank you for contacting us”;
$autoreply = “Thank you for contacting us. Somebody will get back to you as soon as possible, usually within 48 hours. If you have any more questions, please consult our website at www.keystonecoaching.co.za”;

if($from == ‘’) {print “You have not entered an email, please go back and try again”;}
else {
if($name == ‘’) {print “You have not entered a name, please go back and try again”;}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{header( “Location: http://www.keystonecoaching.co.za/test/confirm.htm” );}
else
{print “We encountered an error sending your mail, please notify [email protected]”; }
}
}
?>

Untitled Document

Hi there,

Try putting the code before the doctype declaration (so that php is the first thing in the file). If that doesn’t help you may need to resort to javascript and/or meta refresh. FYI those methods are below:

Javascript

window.location = 'http://www.mysite.com/';

HTML(meta)

<meta http-equiv="refresh" content="0;url=http://www.mysite.com/" />

If you are using header(“Location: page”); in your script, you have to make sure of one thing…

there should not be a single output… (not even a blank space) before that line. that is because of…

when header(“location…” is called, the script engine tries to resend the header of the response to the target page. but if the script prior to your header(“location”) statement yields any output (even a blank space), the engine creates a header for that output and starts sending it. so, when executing header(“location”) it cannot redirect the header, coz it already started a header and sent it to the response. thats what it tell you thru the error also. “header already sent…”

so, simple, make sure that before to header(“location”), there is no html content… and blank spaces, enter keys etc…
only php scripts (that to which dont yield any output) are allowed before header (“location”) line.

Coming to the solution provided by Smokey PHP… it works fine but has few issues also…

when u use window.location… the response has to reach the client and then go back to the target page (which is a two way process). instead the redirect is supposed to happen on the server itselft. besides, if the browser doesnt support javascript or disabled it… then the redirect wont happen.

same is the case with meta based refresh… the redirect happens on the client system and not all browsers support it…

Hope i made you clear on this…

Regards,

Sponsor our Newsletter | Privacy Policy | Terms of Service