javascript - ContentEditable 不允许发送 php 字符串

标签 javascript jquery html ajax

我对 contenteditable 有疑问。

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

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

DEMO

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

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

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

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

最佳答案

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

关于javascript - ContentEditable 不允许发送 php 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43117993/

相关文章:

javascript - 提交按钮没有任何作用

javascript - 如何使用 jQuery Mobile 进行倒计时?

html - 在 Bootstrap 中,如何将页面拆分为 2 列,并且左侧部分具有固定宽度

javascript - 从 javascript 节点对象生成无序列表

javascript - JS : why this object property won't work and it only works if I put it inside one of the methods as a variable?

javascript - 计算值的变化

javascript - 如何将来自 ajax 的响应定位到被单击的特定 div

javascript - jQuery .hasClass() 选项卡更改失败

javascript - 如何将 DIV 的大小调整为小于其初始宽度和高度?

javascript - Chart.js 在标签中使用很棒的字体