如何将错误从php脚本发回页面

时间:2011-06-07 19:13:23

标签: php ajax forms

我有一个简单的联系表单,它使用jquery发布到发送电子邮件的php页面。如果未正确输入,我希望将“安全码”错误发回html页面。我该怎么做?

这是PHP脚本:

session_start();

if(($_SESSION['security_code'] != $_POST['security_code']) ||   (empty($_SESSION['security_code'])) ) {
 $return['error'] = true;
 $return['msg'] = 'Please re-enter the security code .';
}
else {
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
        $email = stripslashes(strip_tags($_POST['email']));
    } else {$email = 'No email entered';}
    if ((isset($_POST['phone'])) && (strlen(trim($_POST['phone'])) > 0)) {
        $phone = stripslashes(strip_tags($_POST['phone']));
    } else {$phone = 'No phone entered';}
    if ((isset($_POST['comments'])) && (strlen(trim($_POST['comments'])) > 0)) {
        $comments = stripslashes(strip_tags($_POST['comments']));
    } else {$comments = 'No comments entered';}

    $contactByPhone = $_POST['contactByPhone'];
    $contactByEmail = $_POST['contactByEmail'];
    $contactNoPreference = $_POST['contactNoPreference'];
    $state = $_POST['state'];

    $email_to = "clinic@gmail.com";
    $email_subject = "contact";  

    $email_message .= "Name: ".$name."<br/>";
    $email_message .= "Email: ".$email."<br/>";
    $email_message .= "Phone: ".$phone."<br/>";
    $email_message .= "State: ".$state."<br/>";
    $email_message .= "Comments: ".$comments."<br/>";

    $email_message .= 'Contact By: '.$contactByPhone . '&nbsp;' . $contactByEmail . '&nbsp;' . $contactNoPreference."<br/>";
    $email_message .= $_SERVER['HTTP_HOST'];

    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email."\r\n";
    $headers  .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail($email_to, $email_subject, $email_message, $headers);  

    unset($_SESSION['security_code']);
 }

这是javascript文件:

$(function() {  
 $('.error').hide();  
 $(".button").click(function() {    

$('.error').hide();  
  var name = $("input#name").val();  
    if (name == "") {  
  $(".error").show();    
  return false;  
}  
    var email = $("input#email").val();  
    if (email == "") {  
  $(".error").show(); 
  return false;  
}  
    var phone = $("input#phone").val();  
    if (phone == "") {  
  $(".error").show();    
  return false;  
}  
var comments = $("#comments").val();  
    if (comments == "") {  
  $(".error").show();    
  return false;  
}  
var security_code = $("#security_code").val();  
    if (security_code == "") {  
  $(".error").show();    
  return false;  
}  
var state = $("select#state option:selected").val();
var contactByPhone = $("#contactByPhone:checked").val();
var contactByEmail = $("#contactByEmail:checked").val();
var contactNoPreference = $("#contactNoPreference:checked").val();


var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone + '&state=' + state + '&comments=' + comments + '&contactByPhone=' + contactByPhone 
+ '&contactByEmail=' + contactByEmail + '&contactNoPreference=' + contactNoPreference;   
    $.ajax({  
      type: "POST",  
      url: "send_form_email.php",  
      data: dataString,  
      success: function() { 
        //alert (dataString); 
        $('#contact_form').html("<h2>Contact Form Submitted!</h2><br/><p>We will be in touch soon.</p>");    
      }  
    });  

    return false;
    });  
});

2 个答案:

答案 0 :(得分:1)

如果您替换

success:function()...

success:function(data)
然后,无论你在PHP脚本中回应什么,都会在数据变量中。 因此,您可以回显错误信息并在此函数中处理它。

答案 1 :(得分:0)

执行echo json_encode($results)输出错误消息。然后让您的Javascript检查成功处理程序中data['error']是否设置为true。

有些人会说你应该输出一个400或者其他类似的HTTP状态代码来表示错误,但我非常不喜欢这种做法。关于HTTP连接的一切都很好。发出请求,脚本运行,生成响应。 HTTP层的所有内容都运行良好,因此在此级别指示错误是一个愚蠢的想法。这就像说你的汽车旅行完全失败了,因为商店没有你想要的口香糖。