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

如何通过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

图片转代码服务由CSDN问答提供 功能建议

我使用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 ['d  atastring'] ['newEmailForm'] [0] [1]; 
 $ messagetext = $ _POST ['datastring'] ['newEmailForm'] [0] [2],true); 
 $ properties = $ _POST [  'datastring'] ['properties']; 
 
 //发送电子邮件并获取报告
 $ sent = elegeantSend($ to,$ messagetext,$ properties); 
 
} 
   
 
 

我只是不知道如何将字符串转换为&lt; br&gt; 等。

TIA

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douyigua5381 2018-03-13 11:17
    已采纳

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drjk87189 2018-03-13 10:36

    you can use str_replace

    example:

    $messagetext = $_POST['datastring']['newEmailForm'][0][2], true);
    $convertedMessageText = str_replace('%0A', "
    ", $messagetext);
    

    PS: this is for replace %0A to new line (like the question), not the other

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题