简体   繁体   English

Javascript / Php:设置textarea值

[英]Javascript / Php : set textarea value

I have the following script, which returns 2 values from my database. 我有以下脚本,它从我的数据库返回2个值。

$(document).ready(function() {
    <?
    $link=new mysqli($serveur,$login,$pass,$base);
    mysqli_set_charset($link, "utf8");

    $r=mysqli_query($link, "select sujet,corps from mail where type='Création_rationnaire'");
    while($row = mysqli_fetch_array($r)) {
    ?>
        document.getElementById("sujet").value="<? echo $row[sujet];?>";
        document.getElementById("te").value="<? echo $row[corps];?>";
    <? }mysqli_close($link); ?>
});

Here are my 2 forms, one is a classic Input, the other is a textarea. 这是我的两种形式,一种是经典输入,另一种是textarea。

<input id='sujet' class="form-field">
<textarea id="te" class="textarea" rows="9" cols="70"></textarea>

The problem is : my SQL request works in MySQL, but when "corps" is echoed as above, nothing appears in the Inputs, like the script was crashing... Only "sujet" can be echoed successfully. 问题是:我的SQL请求在MySQL中工作,但是当“corps”如上所述回响时,输入中没有任何内容出现,就像脚本崩溃一样......只有“sujet”才能成功回显。 So... why, just why ? 那么......为什么,为什么呢? Thanks in advance for whoever helps me. 提前谢谢谁帮助我。

EDIT : all I can echo in the "te" textbox is a single string like this : echo 'something'; 编辑:所有我能在“te”文本框中回显的是这样的单个字符串: echo 'something'; ... but nothing else works... ......但没有其他工作......

Try this 尝试这个

document.getElementById("sujet").value="<? echo $row['sujet'];?>";
document.getElementById("te").text="<? echo $row['corps'];?>";

or if you use jquery 或者如果你使用jquery

var message = <?php echo $row['corps']; ?>;
$("#te").val(message);    

Textarea does not have a value but it does have text. Textarea没有值,但确实有文本。

in an input box you would have <input type="text" value="example" /> and in a text area the value is inside the element like so 在输入框中,您将具有<input type="text" value="example" />并且在文本区域中,值在元素内部,如此

<textarea>example</textarea>

A other way is to use innerHtml (javascript) or html (jquery) . 另一种方法是使用innerHtml(javascript)或html(jquery)。 But note that html tags will be display with one of these options 但请注意,html标签将显示其中一个选项

document.getElementById("te").innerHTML="<? echo $row['corps'];?>";

<textarea> doesn't have value property. <textarea>没有value属性。 Since you are using jQuery to bind document ready, you can use jQuery to set the value. 由于您使用jQuery来绑定文档,因此可以使用jQuery来设置值。

$("#sujet").val("<? echo $row[sujet];?>");
$("#te").html("<? echo $row[corps];?>");

What is the value of $row[corps]? $ row [corps]的价值是多少? Is it empty string? 它是空字符串吗? Does it contain double quote, or newline characters? 它是否包含双引号或换行符? If it contains any of them, it will break your javascript source code. 如果它包含任何一个,它将破坏您的JavaScript源代码。 What does javascript source code look like when you check source code inside the browser? 当您在浏览器中检查源代码时,javascript源代码是什么样的?

It works by inserting the values directly into the plain html forms. 它的工作原理是将值直接插入到普通的html表单中。 I usually use this javascript function because I have tons of forms to fill in my program, and it never failed until now... So my function above was good, but we still don't know why it failed in this particular case... 我通常使用这个javascript函数,因为我有大量的表单来填写我的程序,它直到现在都没有失败...所以我上面的函数很好,但我们仍然不知道为什么它在这种特殊情况下失败了.. 。

Problem solved anyways... thanks to everyone. 无论如何问题解决了......谢谢大家。

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

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