PHP Contact Code Not working - Any help appreciated!

For some reason I cannot get 2 things to happen.

  1. It will not grab the company information unless I add it as required but I don’t want it to be required.
  2. I cannot get it to take the user to the thankyou.html page.

Any help would be appreciated. Code below… thanks:

<?php if(!$_POST) exit; $email = $_POST['email']; //$error[] = preg_match('/\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i', $_POST['email']) ? '' : 'INVALID EMAIL ADDRESS'; if(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email )){ $error.="Invalid email address entered"; $errors=1; } if($errors==1) echo $error; else{ $values = array ('name','company', 'email','message'); $required = array('name', 'email','message'); $your_email = "[email protected]"; $email_subject = "Test Subject: ".$_POST['subject']; $email_content = ""; foreach($values as $key => $value){ if(in_array($value,$required)){ if ($key != 'subject' && $key != 'company') { if( empty($_POST[$value]) ) { echo 'PLEASE FILL IN REQUIRED FIELDS'; exit; } } $email_content .= $value.': '.$_POST[$value]."\n"; } } if(@mail($your_email,$email_subject,$email_content)) { header("Location: thankyou.html"); } else { echo 'ERROR!'; } } ?>

You have a little logic problem in your loop (see my comments):
[php]foreach($values as $key => $value){
if(in_array($value,$required)){
# if ($key != ‘subject’ && $key != ‘company’) {
// You should be testing $value here, not $key
if ($value != ‘subject’ && $value != ‘company’) {
if( empty($_POST[$value]) ) { echo ‘PLEASE FILL IN REQUIRED FIELDS’; exit; }
}

#$email_content .= $value.': '.$_POST[$value]."\n";
/* This line is inside the IF required so it will only be processed for required fields */

} # END if (in_array

/* Put the line here, but now we have to test to see if it exists in $_POST */
$email_content .= $value . ': ’ . (isset($_POST[$value]) ? $_POST[$value] : ‘’) . “\n”;

} # END foreach
[/php]

If you are not getting to the thankyou page, then the mail() function might be failing:
[php]
if(@mail($your_email,$email_subject,$email_content)) {
header(“Location: thankyou.html”);
exit; # ALWAYS place an exit after a header redirect so the script will not continue running
[/php]
Do not use the “@” construct to hide errors. Make sure error reporting is on, and FIX the errors.

If the problems continue, post your new code (use the PHP tags) and explain what IS happening and what is NOT happening.

Sponsor our Newsletter | Privacy Policy | Terms of Service