我已經構建了一個簡單的jQuery AJAX請求,用於查詢選定模板的數據庫,然後返回電子郵件主題和電子郵件正文並將其插入html輸入字段。這一切都很好。jQuery AJAX請求 - 處理來自PHP頁面的特殊字符
我剛剛注意到,當數據被插入到電子郵件正文中,它看起來像這樣的:
Dear Sally & John, thank you for your call today . . .
代替:
Dear Sally & John, thank you for your call today . . .
還有一些在電子郵件正文中的佔位符用戶通常會像這樣手動更換:
Dear <<dear>>,
這些顯示爲:
Dear <<dear>>
我使用PHP來查詢數據庫,並作爲JSON返回數據:
$templateDetails[] = array('templateBody' => $updatedTemplateBody);
echo json_encode($templateDetails);
下面是調用PHP請求腳本:
$(document).ready(function() {
$("#templateRef").change(function() {
var templateRef = $("#templateRef").val();
var contactID = '<?php echo $contactID; ?>';
$.post('getSMSTemplate.php', {
contactID: contactID,
templateID: templateRef
}, function(data) {
data = JSON.parse(data);
if (data.error) {
alert("error");
$("#messageBody").html('');
return; // stop executing this function any further
} else {
// console.log(data[0].templateBody);
$("#messageBody").val(data[0].templateBody);
}
}).fail(function(xhr) {
$("#messageBody").html('');
});
});
});
有一些功能我可以打電話來逃避這些角色?不知道這是用JavaScript還是PHP完成,或者從哪裏開始?
你的'messageBody'是一個文本框還是一個'div'?因爲你使用'html()'用於'div'和'val()',它用於文本框。假設你的代碼正在工作,我認爲它是一個文本框,否則你不會看到任何結果。 –