Jquery .ajax联系表单有效,但不会成功

时间:2011-08-23 21:06:07

标签: jquery ajax

我正在测试使用jquery和ajax创建一个电子邮件表单。电子邮件处理并发送正常,但.ajax中的成功函数不会触发。我对所有这些功能都很陌生,所以我不确定会出现什么问题。我非常感谢你的帮助! fyi(#blah)只是一个指标

    $(function() {  
  $(".button").click(function() {  
    // validate and process form here 
  $('#blah').hide();
      var name = $("input#name").val();  
        var email = $("input#email").val();  
        var phone = $("input#phone").val();   
    var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;  
//alert (dataString);return false;  
$.ajax({  
  type: "POST",  
  url: "http://www.joepolitic.com/contactform.php",  
  data: dataString,  
  success: function() {  
    $('#second').hide(); 
  }  
});  
return false;  

  });  
});  

PHP:

<?php

// get posted data into local variables
$EmailFrom = "First Facility Contact Form";
$EmailTo = "xxx@xxx.com";
$Subject = "Contact";
$Name = Trim(stripslashes($_POST['name'])); 
$Email = Trim(stripslashes($_POST['email'])); 
$Phone = Trim(stripslashes($_POST['phone'])); 

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Phone: ";
$Body .= $Phone;
$Body .= "\n";


// send email 
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");


?>

1 个答案:

答案 0 :(得分:1)

尝试修改代码以包含以下内容:

  success: function(result) {
    alert(result);  
    $('#second').hide(); 
  },
  error:function (xhr, ajaxOptions, thrownError){
    alert(xhr.statusText);
  }