林解析使用Ajax PHP与来自数组中的值发送电子邮件数组时有困难。阿贾克斯阵到PHP问题
Ajax代码:
$(document).ready(function(){
$("#submit-button").click(function(){
var countryArray = ['Location Zero', 'Location One', 'Location Two'];
dataString = countryArray;
var jsonString = JSON.stringify(dataString);
$.ajax({
type: "POST",
url: "sendmail.php",
data: {countries: jsonString},
success: function (msg) {
$("#errors").text("Thank you for getting in touch, we will get back to you!");
},
error: function (msg) {
$("#errors").text("Error sending email, please try again.");
alert("error");
}
});
});
});
PHP代码:
<?php
$to = "[email protected]";
$countries = json_decode($_POST['countries']);
$header = "Content-Type: text/html\r\nReply-To: \r\nFrom: <>";
$subject = "Email from the Lister customer";
$body = @"$countries";
if(mail($to, $subject, $body, $header)) {
die("true");
} else {
die("There was an error sending the email.");
}
?>
但是,所有我与收到的电子邮件从$countries
是单词 “阵列”,而不是价值。
任何人都可以帮忙吗?
'$ countries'是一个数组,因此使用'破灭( “”,$国家);'将其打印为一个字符串。 – Joe
'@“$ countries”'?不要压制错误,也不要使用货物 - 邪教编程... –
谢谢你们,现在所有的工作都完美:) – medzi