Hi everybody, I will be very grateful if anyone can help: I have a form in html run by php. Once a person has filled in the form and if there are no mistakes, I want php to display my index page. here is the code i have been using but it just doesnt seem to work! if (mail($recipient,$subject,$msg,$header)){ header( 'refresh: 0; url=http://www.example.com' ); } The form works perfect but it does not automatically redirect to the page i want after filling the form. Please help! Cheers!
It should look like this: if (mail($recipient,$subject,$msg,$header)) { header('Location: http://www.example.com'); } Code (markup):
Try add an 'else' block. Probably the mail() function fails. The correct header to use is the one posted by adultuserbars.
Thanks for the reply guys. the mail works perfectly well, but the header command doesnt. I've added the code here. I would be very grateful if someone clould have a look at it. Cheers! <?php } else { error_reporting(0); // initialize a array to //hold any errors we encounter $errors = array(); // test to see if the form was actually // posted from our form $page = $_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']; if (!ereg($page, $_SERVER['HTTP_REFERER'])) $errors[] = "Invalid referer<br>\n"; // check to see if a name was entered if (!$_POST['last_name']) $errors[] = "Please enter your last name"; if (!$_POST['first_name']) $errors[] = "Please enter your first name"; if (!$_POST['email']) $errors[] = "Please enter a valid email"; if (!$_POST['hear']) $errors[] = "Please tell us how you heard of us"; if (!$_POST['comments']) $errors[] = "Please enter your comments"; // if there are any errors, display them if (count($errors)>0) { foreach($errors as $err) echo "$err<br>\n"; echo "<br>Please use your browsers Back button to fix."; } else { // no errors, so we build our message $recipient = 'info@abc.com'; $subject = "Website Enquiry Form"; $from = stripslashes($_POST['name']); $em = stripslashes($_POST['email']); $msg = "Message sent by $from\n"; $header = "From: ".$from." <".$em.">"; $msg.="\nLast Name: ".stripslashes($_POST['last_name'])."\n"; $msg.="\nFirst Name: ".stripslashes($_POST['first_name'])."\n"; $msg.="\nCompany: ".stripslashes($_POST['company'])."\n"; $msg.="\nAddress: ".stripslashes($_POST['address'])."\n"; $msg.="\nTown/City: ".stripslashes($_POST['town'])."\n"; $msg.="\nPostcode: ".stripslashes($_POST['postcode'])."\n"; $msg.="\nPhone: ".stripslashes($_POST['telephone'])."\n"; $msg.="\nEmail: ".stripslashes($_POST['email'])."\n"; $msg.="\nHow did you hear of us?: ".stripslashes($_POST['hear'])."\n"; $msg.="\nComments: ".stripslashes($_POST['comments'])."\n"; if (mail($recipient,$subject,$msg,$header)){ header('Location: http://www.example.com'); } else echo "An unknown error occurred."; } } ?>
You have a number of possible echo's prior to the call to header(). Outputting something to the browser, either by echo/print/etc or by having a blank line or even a space before '<?php' would automatically send HTTP headers to browser (you can't send HTTP content without headers). Once the headers have been sent, you can't send more within the same response. Possible solutions: 1. If you want to output something on that page (e.g. printing error messages), let it output there without redirecting user. 2. Store the output somewhere (e.g. in a session variable), then display it in the redirection target page.
Try these: echo "<script>window.location.href(\"http://www.example.com\");</script>"; instead of header('location:http://www.example.com');