这可能已经得到了解答,但我似乎无法找到解决方案。我很确定我已经在互联网上了。
我尝试使用AJAX从表单发送邮件。 AJAX脚本似乎工作正常。这是代码:
$("#order").submit(function(e) {
e.preventDefault();
$(".loading-text").fadeIn("500");
var title = $("input#title").val();
var price = $("input#price").val();
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
var address = $("input#address").val();
var urgency = $("input#urgency").val();
$.ajax({
url: "assets/order.php",
type: "POST",
dataType: "json",
data: {
title: title,
price: price,
name: name,
email: email,
phone: phone,
address: address,
urgency: urgency
},
cache: false,
processData: false,
success: function(data) {
$(".loading-text").fadeOut("300");
$(".success-text").fadeIn("500").append(data);
},
error: function(data) {
$(".loading-text").fadeOut("300");
$(".failure-text").fadeIn("500").append(data);
}
});
});
但它返回所有变量的undefined_index。这是PHP脚本:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = htmlspecialchars($_POST['name']);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$title = htmlspecialchars($_POST['title']);
$price = htmlspecialchars($_POST['price']);
$phone = htmlspecialchars($_POST['phone']);
$address = htmlspecialchars($_POST['address']);
$urgency = htmlspecialchars($_POST['urgency']);
$to1 = "jerryasih@gmail.com";
$to2 = "shalomdickson@yahoo.com";
$subject = "New order received!";
$msg_stalker = "Hello STALKER! New order received.\n\n"."Here are the details:\n"."Name: $name\n\nAddress: $address\n\nEmail: $email\n\nPhone number: $phone\n\nItem: $title\n\nPrice: $price\n\nThis customer needs their service/product in $urgency hours.";
$msg_owner = "Hello! Your store on STALKER! has a new order.\n\n"."Details:\n"."Item: $title\n\nPrice: $price\n\nThis customer needs their service/product in $urgency hours."."You may contact us on hoodstalker@yahoo.com if item is unavailable or for any other query.\n\nExpect us soon.\n\nWe've got your back! ;)";
$from = "Order form at (stores.neighbourhoodstalker.com)";
if (empty($name) || empty($address) || empty($phone)) {
http_response_code(400);
echo "Error! Please check that all required fields have been filled and try again.";
exit;
}
if(mail($to1, $subject, $msg_stalker, 'From: '.$from) && mail($to2, $subject, $msg_owner, 'From: '.$from)) {
http_response_code(200);
echo "Order placed successfully.";
}
else {
http_response_code(500);
echo "Something went wrong. Check your internet connection and try again.";
}
}
else {
http_response_code(403);
echo "There was a problem with your submission. Please try again.";
}
?>
我不确定问题现在在哪里,因为我为文件上传器使用了类似的代码并且它运行良好。
答案 0 :(得分:2)
问题是由于AJAX中的这个设置,
processData: false,
processData (默认:
true
)类型:布尔值
默认情况下,
data
作为对象传入数据选项(技术上,除字符串以外的任何东西)将被处理并转换为查询字符串,适合默认内容类型“application / x -www窗体-urlencoded”。 如果要发送DOMDocument或其他未处理的数据,请将此选项设置为false
。
由于您没有发送任何DOMDocument或未处理的数据,请从您的AJAX请求中删除此设置processData: false
。
答案 1 :(得分:1)
您使用了错误的dataType。使用dataType:“json”。