【问题标题】:ContentEditable doesn't allow to send php stringsContentEditable 不允许发送 php 字符串
【发布时间】:2017-03-30 12:32:57
【问题描述】:

我对 contenteditable 有疑问。

在我使用 textarea 发送文本帖子之前。我现在想更改它以使用 contenteditable。

所以 contenteditable 不允许从数据中发送 php 字符串。例如

DEMO

在这个演示中,我将向您展示我的 html 和 ajax 代码。如果用户发送普通文本,那效果很好。但是如果用户想从他的朋友那里分享 php 代码,比如 (<?php echo 'Hi There.';?>),那么问题就来了。如果用户写像 (Hi There.) 这样的普通文本,则没有任何问题。

通常我们使用 val(); 如果我们像这样使用 textarea

var updateText = $('#post-text-area').val();

我认为这里会出现问题,但我不确定。

【问题讨论】:

    标签: javascript jquery html ajax


    【解决方案1】:

    encodeURIComponent()一起使用。它将阻止标签从html传递到php。并从php中检索数据与decodeURIComponent()一起使用

    $(document).ready(function() {
    
    
          $("body").on("click", ".sendPost", function() {
            var updateText = $('#post-text-area').html();
            var dataString = 'update=' +  encodeURIComponent(updateText);
            $.ajax({
              type: 'POST',
              url: '/posttext.php',
              data: dataString,
              beforeSend: function() {},
              success: function(html) {
               decodeURIComponent(html);
              }
            });
          });
    

    【讨论】:

    • 所以它现在正在发送 php 代码但为什么我看到这样的代码<?php echo $ssss;?><p></p>
    • 将字符串编码为另一种格式。参见article其显示编码格式
    • 我理解并解决了它。感谢您的回答。你救了我。
    猜你喜欢
    • 1970-01-01
    • 2011-05-31
    • 2019-06-01
    • 1970-01-01
    • 2015-10-04
    • 1970-01-01
    • 2012-11-15
    • 2015-07-17
    • 2021-05-13
    相关资源
    最近更新 更多