duan5362
2016-03-29 15:48 阅读 66
已采纳

PHP电子邮件脚本在验证后无法运行

There must be thousands of questions asked about PHP email scripts but I still haven't found my answer. I looked on the php website but it didn't really help either.

Basically my script validates the inputs and displays error message accordingly but when I add the second part of the script to handle the sending of the email the page displays a 500 error. Here is my code. The email hasn't been set to the correct email at the moment but I know that's not the problem.

<?php include('includes/header.php'); 

$yourEmail = "hello@example.com";
$formSubject = "Message recieved from enquiry form";


if( isset( $_POST["submit"])) {

//Validate the data
function validateFormData( $formData ) {
    $formData = trim( stripslashes(htmlspecialchars( $formData )));
    return $formData; 
}


//check to see if inputs are empty
// create variables with form data
// wrap the data with our function

if( !$_POST['name']) {
    $nameError = "Please enter your name <br />";  
} else {
    $name = validateFormData( $_POST["name"]);
}
if( !$_POST['email'] || !preg_match("/^\S+@\S+$/", $email)) {
    $emailError = "Please enter a valid email address <br />";  
} else {
    $email = validateFormData( $_POST["email"]);
}                
if( !$_POST['enquiry']) {
    $enquiryError = "Please enter your enquiry <br />";  
} else {
    $enquiry = validateFormData( $_POST["enquiry"]);
} 

Code Works fine up until this point;

//Check to see if submit is set
//Check to make sure errors don't exist
//Send the email
// Redirect user to success.html  


if ( isset( $_POST["submit"]) && (empty($emailError, $nameError, $enquiryError))) {

$to = $yourEmail;
$subject = $formSubject;  
$headers = 'From:'. $name . "
".
    'Reply-To: '. $email . "
".
    'X-Mailer: PHP/' . phpversion();

$body = 

'The person that contacted you was '.$name.'
Email: '.$email.'
URL: '.$url.'
Message: '.$enquiry.''.

mail($yourEmail, $formSubject, $body, $headers);



header("Location: success.html");


}
}

?>

This is the HTML form, which I'm sure is alright considering the PHP works up until a certain point and displays the error messages correctly. I know I have PHP_SELF which a lot of people say not to use but I want to to stay on the page if errors exist and only redirect if email has sent.

<form id="contactForm" method="POST" action="<?php echo   htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<label for="name">Name&nbsp;<span class="asterisk">*</span></label> 
 <input type="text" name="name" id="name" value="<?php echo $name;?>"tabindex="1"/>
<span class="error"><?php echo $nameError; ?></span>

<label for="email">Email&nbsp;<span class="asterisk">*</span></label> 
 <input type="email" name="email" id="email" value="<?php echo $email;?>" tabindex="2"/>
  <span class="error"><?php echo $emailError; ?></span>  
  <span class="error"><?php echo $emailValidError; ?></span>  

 <label for="url">Website</label> 
  <input type="url" name="url" id="url" value="<?php echo $url;?>" tabindex="3"/>


 <h3>Tell us about your project&nbsp;</h3><br />

 <textarea name="enquiry" id="enquiry" tabindex="4"><?php echo $enquiry;?></textarea>
   <span class="error"><?php echo $enquiryError; ?></span>  

   <button type="submit" id="submit" name="submit" value="submit" >Submit</button>

 </form>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongnuo6310 dongnuo6310 2016-03-29 16:23

    Your problem is here:

    empty($emailError, $nameError, $enquiryError)
    

    empty accepts only one param, you are passing it 3. Change the conditional to:

    if( isset( $_POST["submit"]) && ( empty($emailError) && empty($nameError) && empty($enquiryError) ))
    

    Or if you want to shorten that conditional a little

     $Errors = $emailError.$nameError.$enquiryError;
     if( isset( $_POST["submit"] ) &&  empty( $Errors ) )
    
    点赞 评论 复制链接分享
  • dongying195959 dongying195959 2016-03-29 17:14

    This is why there's an error. First syntax error and then (empty($emailError, $nameError, $enquiryError)) is not a good idea. I would do something like this:

     $err=0;
     if( !$_POST['name']) {
             $nameError = "Please enter your name <br />";  
             $err++;
           } else {
            $name = validateFormData( $_POST["name"]);
             $err++;
           }
    
     if( !$_POST['email'] || !preg_match("/^\S+@\S+$/", $email)) {
           $emailError = "Please enter a valid email address <br />"; 
           $err++; 
           } else {
            $email = validateFormData( $_POST["email"]);
      }        
    
    
     if( !$_POST['enquiry']) {
          $enquiryError = "Please enter your enquiry <br />";  
          $err++;
          } else {
          $enquiry = validateFormData( $_POST["enquiry"]);
        } 
    
       if ( isset( $_POST["submit"]) && $err==0) {
            //your code here//
            //and i would do like this//
            $result_mail_send = mail($yourEmail, $formSubject, $body, $headers);
            if($result_mail_send==true){
                  //redirect to success
            }else{
                  //redirect to fail//
            }
    
        }
    
    点赞 评论 复制链接分享

相关推荐