gpt4 book ai didi

php - 使用 Ajax 和 Swiftmailer 提交表单

转载 作者:行者123 更新时间:2023-12-01 05:46:26 25 4
gpt4 key购买 nike

我正在使用 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);
}

?>

最佳答案

表单不需要名称。给它一个 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());
});
});

关于php - 使用 Ajax 和 Swiftmailer 提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26349119/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com