Hi,
I have a problem with the following script that I'm using on a form page:
<?
function check_telephone($telephone){
if(!preg_match("/[^0-9\ ]+$/",$telephone)) return true;
else return false;
}
function check_postcode($postcode){
if(!preg_match("^[ .a-zA-Z0-9:-]{1,150}$",$postcode)) return true;
else return false;
}
function check_email_address($emailaddress) {
// First, we check that there's one @ symbol, and that the lengths are right
if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $emailaddress)) {
// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("@", $emailaddress);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
return false;
}
}
if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
return false;
}
}
}
return true;
}
// only validate form when form is submitted
if(isset($submit_button)){
$error_msg='';
if($checkbox!=="checkbox"){
$error_msg.="You must agree to the Terms and Conditions<br />";
}
if($title=='') {
$error_msg.='Please enter a Title<br />';
}
if($first_name=='') {
$error_msg.='Please enter your First Name<br />';
}
if($last_name=='') {
$error_msg.='Please enter your Last Name<br />';
}
if($address_1=='') {
$error_msg.='Please enter the first line of your Address<br />';
}
if($postcode==''){
$error_msg.='Please enter your Postcode<br />';
}
else if (!check_postcode($postcode)){
$error_msg.='Please enter a valid Postcode<br />';
}
if($telephone=='') {
$error_msg.='Please enter your Telephone Number<br />';
}
else if(!check_telephone($telephone)){
$error_msg.='Please enter a valid Telephone Number<br />';
}
if($email=='') {
$error_msg.='Please enter your Email Address<br />';
}
else if(!check_email_address($email)) {
$error_msg.='Please enter a valid Email Address<br />';
}
// display error message if any, if not, proceed to other processing
if($error_msg==''){
$email = $HTTP_POST_VARS[email];
$mailto = "me@mysite.com";
$mailsubj = "Download Form Details";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values submitted from web site download form:\n\n";
$redirect = "index.html";
while (list ($key, $val) = each ($HTTP_POST_VARS)) { $mailbody .= "$key : $val\n"; }
if (!eregi("\n",$HTTP_POST_VARS[email])) {
mail($mailto, $mailsubj, $mailbody, $mailhead);
header("Location: thankyou.html");
exit; }
}
}
?>
The following code is then placed above the form:
<? if (!$error_msg=='') echo "<font color=red><p><strong>Please correct the folllowing errors before submitting form:</strong></p><p>$error_msg</p></font>" ?>
The form action returns it back to this page.
My main problem is that whilst it emails the details absolutely fine I cannot get it to redirect to a new page (thankyou.html) and I keep getting a warning about the line on which the following code resides
header("Location: thankyou.html");
It says: "Cannot modify header information - headers already sent on "(and then references the line on which the above code is)
Can anybody see what is wrong?
Any help much appreciated.