提交表单时不调用成功函数

时间:2010-06-08 18:18:23

标签: php jquery ajax

我一直试图弄清楚为什么以下脚本的成功函数没有运行。我表单中的所有内容都完美无缺,表单内容正在通过电子邮件发送,但成功函数未被调用。

如果有人可以查看我的代码并让我知道为什么我的成功功能没有被调用,我会非常感激!

这是带有通知div的HTML表单,它们通过css隐藏:

<div id="success" class="notification">
  <p>Thank you! Your message has been sent.</p>
</div>
<div id="failure" class="notification">
  <p>Sorry, your message could not be sent.</p>
</div>
<form id="contact-form" method="post" action="" class="jqtransform">
  <label for="name">Name:</label>
  <input name="name" id="name" type="text" class="validate[required] input" />
  <label for="company">Company:</label>
  <input name="company" id="company" type="text" class="input" />
  <label for="phone">Phone:</label>
  <input name="phone" id="phone" type="text" class="input" />
  <label for="email">Email:</label>
  <input name="email" id="email" type="text" class="validate[required,email] input" />
  <div class="sep"></div>
  <label for="subject">Subject:</label>
  <input name="subject" id="subject" type="text" class="validate[required] input" />
  <div class="clear"></div>
  <label for="message">Message:</label>
  <textarea name="message" id="message" class="validate[required]"></textarea>
  <div id="check-services">
    <input type="checkbox" name="services[]" value="Contractor Recommendation" />
    <div>Contractor Recommendation</div>
    <input type="checkbox" name="services[]" value="Proposal Review" />
    <div>Proposal Review</div>
    <input type="checkbox" name="services[]" value="Existing Website Review" />
    <div>Existing Website Review</div>
    <input type="checkbox" name="services[]" value="Work Evaluation" />
    <div>Work Evaluation</div>
    <input type="checkbox" name="services[]" value="Layman Translation" />
    <div>Layman Translation</div>
    <input type="checkbox" name="services[]" value="Project Management" />
    <div>Project Management</div>
  </div>
  <div class="sep"></div>
  <input name="submit" id="submit" type="submit" class="button" value="Send" />
  <input name="reset" id="reset" type="reset" class="button" value="Clear" onclick="$.validationEngine.closePrompt('.formError',true)" />
</form>

这是javascript:

// CONTACT FORM VALIDATION AND SUBMISSION
$(document).ready(function(){
 $('#contact-form').validationEngine({
  ajaxSubmit: true,
  ajaxSubmitFile: 'lib/mail.php',
  scroll: false,
  success:  function(){
   $('#success').slideDown();
  },
  failure: function(){
   $('#failure').slideDown();
   $('.formError').animate({
    marginTop: '+30px'
   });
  }
 });
});

这是我的PHP邮件脚本:

<?php
$name = $_POST['name'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$services = $_POST['services'];

$to = 'contact@tomhartmanconsultant.com';
$subject = 'THC - Contact';

$content .= "You received a message from ".$name.".\r\n\n";
if ($company): $content .= "They work for ".$company.".\r\n\n";
endif;
$content .= "Here's the message:\r\n\n".$message."\r\n\n";
$content .= "And they are interested in the services below:\r\n\n";
$content .= implode("\r\n",$services);
if ($phone): $content .= "\r\n\nYou can reach them at ".$phone.".";
else: $content .= "\r\n\nNo phone number was provided.";
endif;

$headers = "From: ".$name."\r\n";
$headers .= "Reply-To: ".$email."\r\n";

if (mail($to,$subject,$content,$headers)):
 return true;
else:
 return false;
endif;
?>

3 个答案:

答案 0 :(得分:3)

在您的mailerscript中

,回显true或false:您希望浏览器可以读取该内容。此外,调试来回传输的数据(查看firebug)以查看您发送的内容与您获得的内容。

请记住,无论你回应什么,如果有内容,你应该取得成功。成功意味着它连接到您的页面并获得数据 - 失败(尽管非常确定这应该是错误,每个$ .ajax文档)处理请求是否一起失败。

在成功的过程中,您会看到您的返回结果(无论是布尔值,字符串,json等)是否是您想要的结果并从那里开始。

答案 1 :(得分:0)

你不应该在php脚本中设置$isValidate = true来表明验证通过了吗?顺便说一下,从php脚本返回true / false是什么意思?

答案 2 :(得分:0)

我最后只修改了jquery.validationEngine.js文件,以便默认的success参数不检查从php邮件文件返回的任何数据。不是我最喜欢的方式,但它符合我的需要。

感谢大家的帮助!