林解析使用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