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

如何通过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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line