我对 contenteditable 有疑问。
在我使用 textarea 发送文本帖子之前。我现在想更改它以使用 contenteditable。
所以 contenteditable 不允许从数据发送 php 字符串。例如
在这个演示中,我将向您展示我的 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/