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 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 ) )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误