我使用ajax使用swiftmailer提交表单。我在尝试提交表单时遇到错误。我认为它与ajax调用中的“数据”设置有关。在提交不使用ajax的表单时,我可以让swiftmailer邮件脚本正常工作。你可以在这里看到我的作品http://wickbuildings.com/form。我被困住了,任何帮助都会受到赞赏!
这是我的javascript:
$(document).ready(function () {
$("button#send_btn").click(function(){
$.ajax({
type: "POST",
url: "/assets/js/mailers/become-a-builder.php",
data: $('form[name=BecomeBuilderForm]').serialize(),
success: function(msg){
$("#thanks").html(msg) //hide button and show thank you
$("#form-content").modal('hide'); //hide modal
},
error: function(){
alert("failure");
}
});
});
});
和我的swiftmailer邮件脚本:
<?php
//grab named inputs from html then post to #thanks
if (isset($_POST['name'])) {
$email_dsm = strip_tags($_POST['email_dsm']);
$name = strip_tags($_POST['name']);
$business_name = strip_tags($_POST['business_name']);
$address = strip_tags($_POST['address']);
$city = strip_tags($_POST['city']);
$state = strip_tags($_POST['state']);
$zip = strip_tags($_POST['zip']);
$phone = strip_tags($_POST['phone']);
$email = strip_tags($_POST['email']);
$comments = strip_tags($_POST['comments']);
// create message that fills #thanks container
echo "<div class=\"alert alert-success\" >Thank you for your inquiry. " . $email_dsm . " will be following up with you shortly.</div>";
// Create message of email to recipient
$body = "the contents of the email here";
require_once '../plugins/swiftmailer/swift_required.php';
// Create the mail transport configuration
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Create the message
$message = Swift_Message::newInstance();
$message->setTo(array(
'user@somedomain.com'
));
$message->setSubject('my email subject here');
$message->setBody($body, 'text/html');
$message->setFrom($Email);
// Send the email
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
}
?>
答案 0 :(得分:0)
表单不需要名称。给它一个ID。
<form id="BecomeBuilderForm">
然后使用:
$('#BecomeBuilderForm').serialize()
或者如果你真的想使用名称在BecomeBuilderForm周围添加双引号
$('form[name="BecomeBuilderForm"]').serialize()
如果你真的遇到困难,请确保你的表单是使用console.log()
序列化的$(document).ready(function () {
$("button#send_btn").click(function(){
console.log($('form[name="BecomeBuilderForm"]').serialize());
});
});