【发布时间】:2011-12-30 00:46:47
【问题描述】:
问题是: 如何将用户输入的换行符保存到数据库。 (jQuery/php/mysql)
我有一个用户可以编辑的段落。
<p contenteditable="true" id="forbiddenField">Text</p>
当我从数据库中检索数据时:
<p contenteditable="true" id="forbiddenField"><?php echo nl2br($forbidden); ?></p>
效果很好。
onblur (.blur) 将激活一个 jQuery 脚本,它将新编辑的数据发送到数据库。
$("#forbiddenField").blur(function(){
var whatIsNowForbidden = encodeURI($("#forbiddenField").text());
$.ajax({
type: "POST",
url: "updateForbidden.php",
data: "forbiddenStuff="+ whatIsNowForbidden,
success: function(){
}
});
});
问题: 如果用户插入新数据,带有换行符,那么数据库中的数据不会添加换行符,只会添加内容。
我尝试了以下方法: var whatIsNowForbidden = encodeURI($("#forbiddenField").text());
我查看了这些地方的答案: New Line in Textarea to be converted to <br/> http://www.w3schools.com/tags/ref_urlencode.asp Is replacing a line break UTF-8 safe? how to escape input but save unescaped into the database How to recognize every linebreak?
我无法让它工作。 非常感谢所有输入和提示!
【问题讨论】:
-
第一件事:你确定回车没有保存在数据库中吗?另外,您如何使您的段落 (
<p></p>) 可编辑? -
什么占用了换行符的空间?空间?标签?什么都没有?
-
中的 contenteditable="true" 使其可编辑:)
-
请问:“回车”是什么意思?
-
也不确定您所说的“什么占用了换行符的空间?”如果我做一个警报();我得到的段落值: alert(encodeURI($("#forbiddenField").text()));文本: Föbjudet: allt som inte är tillåtet。 (禁止:所有不允许的) 另一行在这里。给出:F%C3%B6bjudet:%20allt%20som%20inte%20%C3%A4r%20till%C3%A5tet.%20(禁止:%20All%20that%20is%20not%20allowed)Another%20line%20down%20here .