gpt4 book ai didi

php - JQuery AJAX表单处理无需刷新

转载 作者:行者123 更新时间:2023-12-01 07:13:56 25 4
gpt4 key购买 nike

我是 JQuery 新手,根本没有使用过 AJAX。我在网上搜索了好几天都没有完整的答案。

我试图通过 AJAX 通过电子邮件发送表单,而 JQuery 隐藏表单并显示附加数据。 JQuery 部分在页面切换时工作以显示正确的数据,但表单信息不会通过电子邮件发送。

JQuery/AJAX

$(document).ready(function() {
//When the form is submitted...
$('form').on('submit',function(e) {
//Send the serialized data to mailer.php.
$.ajax({
url:'mailer.php',
data:$(this).serialize(),
type:'POST',
success:function(data){
console.log(data);
$("#success").show().fadeOut(5000); //=== Show Success Message==
},
error:function(data){
$("#error").show().fadeOut(5000); //===Show Error Message====
}
});
e.preventDefault(); //=== To Avoid Page Refresh and Fire the Event "Click"===
//$.post("mailer.php");
//Take our response, and replace whatever is in the "form2"
//div with it.
$('#form1').hide();
$('#form2').show();
});
});

PHP提交文件mailer.php

<?php          
//Grab Posted Data
$fname = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));

// PREPARE THE BODY OF THE MESSAGE

$message = '<html><body>';

$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>$name</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>$email</td></tr>";
$message .= "<tr><td><strong>Phone:</strong> </td><td>$phone</td></tr>";
$message .= "<tr><td><strong>Address:</strong> </td><td>$address</td></tr>";
$message .= "<tr><td>&nbsp;</td><td>$city, $state $zip $country</td></tr>";
$message .= "<tr><td><strong>Goals:</strong> </td><td>$message</td></tr>";

$message .= "</table>";
$message .= "</body></html>";


// CHANGE THE BELOW VARIABLES TO YOUR NEEDS

$to = 'me@me.com';

$subject = 'Website Change Reqest';

$headers = "From: $email \r\n";
$headers .= "Reply-To: $email \r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

if (mail($to, $subject, $message, $headers)) {
echo 'Your message has been sent.';
} else {
echo 'There was a problem sending the email.';
}

?>

最佳答案

PHP mail() 使用 sendmail 系统,大多数问题都是由于设置不正确而发生的。有时网络托管商会直接禁用它。

有时,我通过使用外部库解决了这个问题,PhpMailer ,例如,使用真实邮件帐户的 SMTP。

附注 - 永远不要这样做:

    $fname   = strip_tags(htmlentities($_POST['fname']));
$lname = strip_tags(htmlentities($_POST['lname']));
$name = $fname." ".$lname;
$email = strip_tags($_POST['email']);
$phone = strip_tags(htmlentities($_POST['phone']));
$address = strip_tags(htmlentities($_POST['address']));
$city = strip_tags(htmlentities($_POST['city']));
$state = strip_tags(htmlentities($_POST['state']));
$zip = strip_tags(htmlentities($_POST['zip']));
$country = strip_tags(htmlentities($_POST['country']));
$message = strip_tags(htmlentities($_POST['goals']));

它可以很容易地被替换为:

     extract(
array_map(
function($elem) {
return strip_tags(html_entities($elem));
}, $_POST)
);

关于php - JQuery AJAX表单处理无需刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21631965/

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