【发布时间】:2017-03-26 09:08:47
【问题描述】:
我使用了一个淡出并显示感谢信息的 ajax 联系表单。我在提交表单后也收到了邮件。但它没有在表单中输入任何数据。我认为表单数据没有传递给 handler.php..有什么帮助吗?
ajax.js
$(function() {
var theForm = $("#memberform");
theForm.validate({
submitHandler: function(theForm) {
$('#loader', theForm).html('Please Wait...');
$.ajax({
type: "POST",
url: "handler.php",
data: $(theForm).serialize(),
timeout: 20000,
success: function(msg) { $(theForm).fadeOut((500, function(){
$(theForm).html("<h2>Thank you. We will contact you shortly.</h2>").fadeIn();
}));
},
error: $('.thanks').show()
});
return false;
}
});
});
handler.php
<?php
$to = 'something@gmail.com';
$subject = 'Contact Form';
$name = $_POST['senderName'];
$phone = $_POST['senderNumber'];
$email = $_POST['senderEmail'];
$message = $_POST['senderComments'];
$MESSAGE_BODY = "Name: ".$name."<br>";
$MESSAGE_BODY .= "Contact No: ".$phone."<br>";
$MESSAGE_BODY .= "Email: ".$email."<br>";
$MESSAGE_BODY .= "Message: ".nl2br($message)."<br>";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'From: Companyname' . "\r\n";
$headers .= 'Reply-To: something.com' . "\r\n";
$headers .= 'Cc: something@gmail.com' . "\r\n";
//$headers .= 'Bcc: example@example.com' . "\r\n";
mail($to, $subject, $MESSAGE_BODY, $headers);
?
我在 body 标签顶部的 head 标签中链接的脚本。
<script src="js/jquery-3.1.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="ajax.js"></script>
<script type="text/javascript" src="met.js"></script>
我使用重写规则编辑了 web.config 文件以隐藏域中的 .php 扩展...
<rewrite>
<rules>
<rule name="Hide .php ext">
<match ignoreCase="true" url="^(.*)"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
<add input="{REQUEST_FILENAME}.php" matchType="IsFile"/>
</conditions>
<action type="Rewrite" url="{R:0}.php"/>
</rule>
<rule name="Redirecting .php ext" stopProcessing="true">
<match url="^(.*).php"/>
<conditions logicalGrouping="MatchAny">
<add input="{URL}" pattern="(.*).php"/>
</conditions>
<action type="Redirect" url="{R:1}"/>
</rule>
</rules>
</rewrite>
【问题讨论】:
-
代替
$(theForm).serialize()试试theForm.serialize()。已经是 jQuery 对象了,不需要再通过 jQuery 传递。 -
嗨,感谢您的帮助。编辑后,ajax 根本不起作用。它重定向到 handler.php 并保持不变。 @maiorano84
-
你序列化的实际表单在哪里?
标签: php ajax contact-form ajaxform