gpt4 book ai didi

javascript - Ajax 不将数据发送到 PHP 文件

转载 作者:行者123 更新时间:2023-12-03 08:47:53 25 4
gpt4 key购买 nike

我编写了这个脚本,它不会将数据从 AJAX 发送到 PHP 文件。在运行 AJAX 函数之前,我通过记录表单中的数据来对其进行调试。它给了我这些数据:

Form: name=jim&email=info%40test.com

但是,我收到了一个空警报,并且收到了一封空电子邮件。

HTML

<form name="form" id="form" class="form" method="post">
<input type="text" class="text border" name="name" id="name" placeholder="Name" />
<input type="text" class="text" name="email" id="email" placeholder="E-mail" />
<input type="submit" class="button" value="Submit" />
</form>

JS

jQuery(function(){
jQuery('#form').submit(function(event){
event.preventDefault();
jQuery.ajax({
type: "POST",
url: "includes/post.php",
data: jQuery('#form').serialize(),

success: function(data){
jQuery("#form").addClass("inactive");
jQuery("#message").addClass("active");
alert(data);
}
});
});
});

PHP

<?php

if ($_SERVER["REQUEST_METHOD"] == "POST") {

$name = mysql_real_escape_string($_POST["name"]);
$email = mysql_real_escape_string($_POST["email"]);

$to = "test@hidden.com";

$message = '

<html>
<body>
<p>
<strong>Name: </strong> '.$name.' <br/>
<strong>E-mail: </strong> '.$email.' <br/>
</p>
</body>
</html>

';

$subject = 'New entry: '.$name.', '.$email;
$headers .= "From: ".$name." ".$$email."\r\n";
$headers .= "X-Mailer: PHP's mail() Function\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";;

mail($to, $subject, $message, $headers);

}

?>

最佳答案

尝试下面的代码,

使用以下内容更新您的 PHP,

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST["name"]);
$email = htmlspecialchars($_POST["email"]);
echo $name;// this will see in your response
$to = "test@gmail.com";
$message = '

<html>
<body>
<p>
<strong>Name: </strong> ' . $name . ' <br/>
<strong>E-mail: </strong> ' . $email . ' <br/>
</p>
</body>
</html>

';
$subject = 'New entry: ' . $name . ', ' . $email;
$headers = "From: " . $name . " " . $email . "\r\n";
$headers.= "X-Mailer: PHP's mail() Function\n";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-Type: text/html; charset=ISO-8859-1\r\n";;
mail($to, $subject, $message, $headers);
}

关于javascript - Ajax 不将数据发送到 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32809364/

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