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