简体   繁体   English

如何将用户输入换行符保存到数据库。 (jQuery / php / mysql)

[英]How to save user input linebreak to database. (jQuery/php/mysql)

The question is: How to save user input linebreak to database. 问题是: 如何将用户输入换行符保存到数据库。 (jQuery/php/mysql) (jQuery / php / mysql)

I have a paragraph which is editable by the user. 我有一个可由用户编辑的段落。

<p contenteditable="true" id="forbiddenField">Text</p>

When I retrieve data from db with this: 当我以此从数据库中检索数据时:

<p contenteditable="true" id="forbiddenField"><?php echo nl2br($forbidden); ?></p>

and that works great. 而且效果很好。

onblur (.blur) will activate a jQuery script which will send the new edited data to a database. onblur(.blur)将激活一个jQuery脚本,该脚本会将新编辑的数据发送到数据库。

$("#forbiddenField").blur(function(){
        var whatIsNowForbidden = encodeURI($("#forbiddenField").text());

        $.ajax({
            type: "POST",  
            url: "updateForbidden.php",  
            data: "forbiddenStuff="+ whatIsNowForbidden,
            success: function(){
            }  
        });
});

Problem: If the user inserts new data, with linebreaks, then the linebreaks are not added to the data in the database, only the content is added. 问题:如果用户插入带有换行符的新数据,则该换行符不会添加到数据库中的数据中,而只会添加内容。

I tried this among other things: var whatIsNowForbidden = encodeURI($("#forbiddenField").text()); 我在其他方面进行了尝试:var whatIsNowForbidden = encodeURI($(“#forbiddenField”)。text());

I looked at theese places for answers: 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? 我在这些地方找到了答案:将Textarea中的新行转换为<br/> http://www.w3schools.com/tags/ref_urlencode.asp 替换换行符UTF-8安全吗? how to escape input but save unescaped into the database How to recognize every linebreak? 如何转义输入但不转义保存到数据库中 如何识别每个换行符?

I can't get it to work. 我无法正常工作。 All input and hints are much appreciated! 非常感谢所有输入和提示!

Try removing the encodeURI() function from your javascript, like so: 尝试从您的JavaScript中删除encodeURI()函数,如下所示:

var whatIsNowForbidden = $("#forbiddenField").text();

I made a fiddle to test this and javascript's encodeURI() function doesn't seem to translate the new lines made in the paragraph to %0A , like we were expecting to. 我做了一个小提琴来测试这一点,而javascript的encodeURI()函数似乎并没有像我们期望的那样将段落中的新行转换为%0A

By removing the function encodeURI() PHP should be able to save the text, including new lines, to the database. 通过删除函数encodeURI() PHP应该能够将文本(包括新行)保存到数据库中。 And then, PHP's nl2br() should transform the new lines into <br /> tag's with no problem. 然后,PHP的nl2br()应该nl2br()将新行转换为<br />标记。

The fiddle is available here: http://jsfiddle.net/LN7QC/ 小提琴可在这里找到: http : //jsfiddle.net/LN7QC/

Final answer: use <textarea> 最终答案:使用<textarea>

The jQuery: jQuery:

$("#forbiddenField").blur(function(){
        var whatIsNowForbidden = $("#forbiddenField").val();

        $.ajax({
            type: "POST",  
            url: "updateForbidden.php",  
            data: "forbiddenStuff="+ whatIsNowForbidden,
            success: function(){
            }  
        });
});

adding to database: 添加到数据库:

<?php
    include('connect.php');

            if($_POST['forbiddenStuff']){
                $forbiddenInput = $_POST['forbiddenStuff'];
            }


    If($forbiddenInput){
        mysql_query("SET NAMES 'utf8'");
        $changeDataInDB = "UPDATE heroes_and_helpers SET forbidden='$forbiddenInput' WHERE name ='sabina'";
        mysql_query($changeDataInDB);

    }
?>

fetching from database: 从数据库中获取:

   mysql_query("SET NAMES 'utf8'");
    $fetchForbidden = mysql_query("SELECT * FROM heroes_and_helpers WHERE name='sabina'");

        if (!$fetchForbidden)
          {
          die('Ngt ar fel: ' . mysql_error());
          }

    $whatIsForbidden = mysql_fetch_array($fetchForbidden); 
    $forbidden = $whatIsForbidden['forbidden'];

on the site: 在网站上:

<textarea id="forbiddenField" style="width: 450px; height: 500px;"><?php echo $forbidden; ?></textarea>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM