我使用 WordPress 的后端来允许客户填写表单并发送电子邮件中文本区域的内容。
我的问题是,当我单击“发送”时,文本区域中的字符串会转换为 Line1%0ALine2%0ALine3
(作为encodeURIComponent()的结果)并将其发布到我的PHP函数。当这通过电子邮件发送出去时,这就是我收到的信息。
我知道我需要以某种方式解码/编码它,我想我已经快到了。
到目前为止我的 JS 看起来像这样。
tinyMCE.triggerSave();
var messagetext = jQuery('textarea#emailMessage').val();
var newEmailForm = [];
var email = jQuery('input.email').val();
var title = jQuery('input.title').val();
var emailMessage = encodeURIComponent(messagetext);
newEmailForm.push([email,title,emailMessage]);
var datastring = {
newEmailForm: newEmailForm,
properties: properties
};
jQuery(function(){
jQuery.ajax({
type:'POST',
data:{
action: 'elegantSendEmail',
datastring: datastring
},
url: ajaxurl,
success: function(data){
alert(data);
}
})
})
使用encodeURIComponent() 发布消息文本,如下所示:Line1%0ALine2%0ALine3
我的 PHP 看起来像。
function elegantSendEmail() {
$to = $_POST['datastring']['newEmailForm'][0][0];
$title = $_POST['datastring']['newEmailForm'][0][1];
$messagetext = $_POST['datastring']['newEmailForm'][0][2], true);
$properties = $_POST['datastring']['properties'];
//Send the email and get a report back
$sent = elegeantSend($to,$messagetext,$properties);
}
我只是不知道如何将字符串转换为 <br>
或\n
等等
TIA
最佳答案
您可以使用rawurldecode函数它将在使用encodeURIComponent()编码的php中进行解码
关于php - 如何通过 ajax 将文本区域中的换行符传递给 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253645/