php - 如何通过 ajax 将文本区域中的换行符传递给 PHP

标签 php jquery ajax

我使用 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/

相关文章:

php - 选择字符串的一部分

php - 在 Stripe 中为一个账户收费

jquery - 将以百分比表示的图像尺寸转换为像素

javascript - 从 jQuery 迁移到原型(prototype)

php - 如何使用 IGDB API 的查询参数格式化 URL?

php - Codeigniter 表单验证外部回调

javascript - 为什么我不能进入我的 $document Ready 函数

javascript - 尝试使用 jQuery.post() 调用 PHP 函数

javascript - Ajax 请求 url 并在 div 中显示响应不起作用?

php - session 变量在不同页面上显示为空