duanlaiquan8174 2018-03-13 10:21
浏览 140
已采纳

如何通过ajax将textarea中的换行符传递给PHP

Im using the back end of wordpress to allow the client to fill in a form and send the content of a text area in an email.

My problem here is that when I click send, the string from the text area converts to Line1%0ALine2%0ALine3 (as a result of encodeURIComponent()) and posts that over to my PHP funciton. When this is sent out in the email thats literally what I get.

I know I need to decode / encode this in some way and I think Im nearly there.

So far my JS looks like this.

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);
            }
        })
    })

Using encodeURIComponent() posts the message text like this: Line1%0ALine2%0ALine3

My PHP Looks like.

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);

}

I just dont know how to convert the string into either <br> or etc.

TIA

  • 写回答

2条回答 默认 最新

  • douyigua5381 2018-03-13 11:17
    关注

    You can use rawurldecode function It will decode in php which is encoded with encodeURIComponent()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这个复选框什么作用?
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决