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 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题
  • ¥15 您好 这个API接口该怎么弄 网站搭建好了 API也有 现在就不知道该怎么填写API 不知道怎么用
  • ¥88 用uniapp写一个多端的程序,用到高德地图,用高德的JSAPI吗?
  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle