Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

My first php file and headers

Status
Not open for further replies.

maxelcat

Technical User
Oct 19, 2006
79
Hello all

I want to send a simple email from a form. That all works very nicely. I then want the browser to redirect (in this case to google). I can't get that bit to work. Could somebody please tell me where I am going wrong.

Thanks

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Survey php</title>
</head>

<body>
<?php
//my first php file!

$to="info@test.co.uk";
$subject = "Customer Satisfaction Survey";


//picks up the names from the form and puts them into strings

$from = "From: " . $_REQUEST['name'];
$email = "email: " . $_REQUEST['email'] . "\n";
$needs = "Needs comment: " . $_REQUEST['needs'] . "\n";
$working = "Working with me: " . $_REQUEST['working'] . "\n";
$quality = "Quality comment: " . $_REQUEST['quality'] . "\n";
$recommend = "Recommendation: " . $_REQUEST['recommend'];


$message = $from ."\n" . $email . "\n" . $needs . "\n" . $working . "\n" . $quality . "\n" . $recommend;

$headers ="From: " . $email;

$sent=mail($to,$subject,$message,$headers);


if($sent) 
{header( "Location: [URL unfurl="true"]http://www.google.co.uk"[/URL] );} 
else 
{print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 




?>
 
You cannot send headers after output has started (see line 1...10).
 
Sorry - so how do I redirect to another page?
 
Either you use client-side redirection (meta tag, javascript) or you do not output anything before you redirect. Think about it. The mail is sent before anything happens on the page and after the email is sent you redirect the user somewhere else. There is no need to output any html, because the user will be redirected.

You need to start your file with the opening php bracket. Make sure there is no print or echo statements before the redirect and no whitespace above the opening php bracket.
 
Took out all the html bits - and guess - worked first time!

cheers
 
Don't know if you're developing on a private server, or you can change any of the php.ini settings...
But in the developing stage for PHP scripts it's handy to let PHP display any errors. PHP would have done this when using header() after html output has been send.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top