简体   繁体   English

将多行文本从数据库加载到TextArea

[英]Loading multiline text from database to TextArea

I have some multi line text saved in MySql database (VARCHAR 255). 我在MySql数据库(VARCHAR 255)中保存了一些多行文本。 When i load it, and process it using standard php function "nl2br", it echoes fine (multi line). 当我加载它,并使用标准的php函数“ nl2br”对其进行处理时,它的回声很好(多行)。 But, when i load multi line text from database, make it "nl2br" and then send it to javascript (so it gets displayed in textarea), it won't be displayed! 但是,当我从数据库加载多行文本时,将其设置为“ nl2br”,然后将其发送给javascript(这样它就会显示在textarea中),它将不会显示! What's wrong? 怎么了?

echo "<SCRIPT>FillElements('".$subject."','".$text."');</SCRIPT>";

PS FillElements function: PS FillElements功能:

function FillElements(Sub,Txt)
        {
            document.getElementById('txtSubject').value=Sub;
            document.getElementById('txtMessage').value=Txt;
        }

Newline tags ( <br /> ) don't cause actual new lines in <textarea> . 换行符( <br /> )不会在<textarea>引起实际的换行。

You can pass the "real" newlines ( \\n ) to your <textarea> , though. 但是,您可以将“实际”换行符( \\n )传递给<textarea>

I created a fiddle for that. 我为此创建了一个小提琴


EDIT: For the updated FillElements code: 编辑:对于更新的FillElements代码:

$subject = "String\nWith\nMultiple\nLines";

printf('<script type="text/javascript">FillElements(%s)</script>',
   json_encode($subject)
);

My guess is that your HTML source code looks like this: 我的猜测是您的HTML源代码如下所示:

<script>FillElements("foo","foo
bar
baz");<script>

Correct? 正确?

In JavaScript, strings cannot span multiple lines... 在JavaScript中,字符串不能跨越多行...

textarea s don't actually store the contents in an attribute like value in the same manner as input elements. textarea实际上并不以与输入元素相同的方式将内容存储在诸如value之类的属性中。 They actually store the contents in in between the <textarea> and </textarea> tags. 它们实际上将内容存储在<textarea></textarea>标记之间。 Meaning that the contents is actually treated as CDATA in the document. 这意味着内容实际上在文档中被视为CDATA。

<textarea>
    This is my Content
</textarea>

Produces a text area with "This is my Content" as the contents. 产生一个以“这是我的内容”作为内容的文本区域。

The implication of this is that you cannot use the code you have to alter the contents of a textarea. 这就意味着您不能使用必须更改文本区域内容的代码。 You have to alter the innerHTML property of the textarea. 您必须更改textarea的innerHTML属性。 I have set up a simple example here: 我在这里建立了一个简单的例子:

http://jsfiddle.net/wFZWQ/ http://jsfiddle.net/wFZWQ/


As an aside, since you are populating the fields using PHP on the creation of the page, why not merely fill the data in the HTML markup, this seems like a long way round to do it. 顺便说一句,由于您是在创建页面时使用PHP填充字段的,所以为什么不仅仅在HTML标记中填充数据,这样做似乎还有很长的路要走。

Also, since you don't appear to be using it, have you seen [jQuery][1] it abstracts alot of things out, so instead of typing document.getElementById("the_id") to get an element you can use CSS selectors and merely write $("#the_id") to get the same element. 另外,由于您似乎没有使用它,因此您看到[jQuery] [1]可以抽象出很多东西,因此您可以使用CSS选择器来代替输入document.getElementById("the_id")来获取元素只需编写$("#the_id")即可获得相同的元素。 You also get a load of useful functions that make writing javascript mucxh easier. 您还将获得许多有用的函数,这些函数使编写javascript mucxh更容易。

[1]: http://jquery.com jQuery [1]: http//jquery.com jQuery

从可编辑文件中插入文本格式<div>或 textarea 进入数据库</div><div id="text_translate"><p>我目前正在为我自己的网站开发一个个人项目,我正在尝试添加将格式化文本存储到数据库中的功能。 到目前为止,我所做的是能够将字体从斜体更改为粗体作为示例,但我完全不知道如何将其传递到数据库。</p><pre> &lt;style&gt; #fake_textarea { width: 100%; height: 200px; border: 1px solid red; } #jBold { font-weigth: bold; } #jItalic{ font-style:italic; } &lt;/style&gt; &lt;script src="/scripts/snippet-javascript-console.min.js?v=1"&gt;&lt;/script&gt; &lt;body&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;button id="jBold"&gt;&lt;b&gt;B&lt;/b&gt;&lt;/button&gt;&lt;button id="jItalic"&gt;&lt;i&gt;I&lt;/i&gt;&lt;/button&gt; &lt;div id='fake_textarea' contenteditable&gt; Select some text and click the button to make it bold... &lt;br&gt;Or write your own text &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jBold').click(function() { document.execCommand('bold'); }); }); &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jItalic').click(function() { document.execCommand('italic'); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 示例工作: <a href="https://codepen.io/goldenowl/pen/KKdZQxY" rel="nofollow noreferrer">codepen</a></p></div> - Insert text format from an editable <div> or textarea into the database

暂无
暂无

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

相关问题 将格式化的文本从文本区域保存到数据库 - Saving formatted text from textarea to database 使用动态下拉列表从MySQL数据库在textarea中加载数据 - Loading data in textarea from MySQL Database using dynamic dropdownlist 我如何将文本从textarea发布到mysql数据库 - How do i post text from textarea to mysql database 将纯文本从文本区域正确存储到MySQL数据库 - Storing pure text from a textarea to a MySQL database correctly 将 textarea 中的文本插入 MySQL 数据库而不丢失格式 - Inserting text from textarea into MySQL database without losing formatting 使用php(撇号)将Textarea中的文本保存在mysql数据库中 - Saving text from Textarea in mysql database with php (apostrophes) 从可编辑文件中插入文本格式<div>或 textarea 进入数据库</div><div id="text_translate"><p>我目前正在为我自己的网站开发一个个人项目,我正在尝试添加将格式化文本存储到数据库中的功能。 到目前为止,我所做的是能够将字体从斜体更改为粗体作为示例,但我完全不知道如何将其传递到数据库。</p><pre> &lt;style&gt; #fake_textarea { width: 100%; height: 200px; border: 1px solid red; } #jBold { font-weigth: bold; } #jItalic{ font-style:italic; } &lt;/style&gt; &lt;script src="/scripts/snippet-javascript-console.min.js?v=1"&gt;&lt;/script&gt; &lt;body&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt; &lt;button id="jBold"&gt;&lt;b&gt;B&lt;/b&gt;&lt;/button&gt;&lt;button id="jItalic"&gt;&lt;i&gt;I&lt;/i&gt;&lt;/button&gt; &lt;div id='fake_textarea' contenteditable&gt; Select some text and click the button to make it bold... &lt;br&gt;Or write your own text &lt;/div&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jBold').click(function() { document.execCommand('bold'); }); }); &lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#jItalic').click(function() { document.execCommand('italic'); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt;</pre><p> 示例工作: <a href="https://codepen.io/goldenowl/pen/KKdZQxY" rel="nofollow noreferrer">codepen</a></p></div> - Insert text format from an editable <div> or textarea into the database 从数据库提取到textarea - Extracting from database to textarea 从数据库输出到textarea - Outputting to textarea from database PHP将textarea文本插入数据库 - PHP insert textarea text into database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM