ALGR
ALGR

Reputation: 319

Javascript / Php : set textarea value

I have the following script, which returns 2 values from my database.

$(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.

<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. 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';... but nothing else works...

Upvotes: 2

Views: 1937

Answers (5)

ALGR
ALGR

Reputation: 319

It works by inserting the values directly into the plain html forms. 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...

Problem solved anyways... thanks to everyone.

Upvotes: 0

pisamce
pisamce

Reputation: 533

What is the value of $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. What does javascript source code look like when you check source code inside the browser?

Upvotes: 1

Szenis
Szenis

Reputation: 4170

Try this

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

or if you use jquery

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

Textarea does not have a value but it does have text.

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

<textarea>example</textarea>

A other way is to use innerHtml (javascript) or html (jquery) . But note that html tags will be display with one of these options

Upvotes: 5

user3118180
user3118180

Reputation:

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

Upvotes: 3

Nikko
Nikko

Reputation: 385

<textarea> doesn't have value property. Since you are using jQuery to bind document ready, you can use jQuery to set the value.

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

Upvotes: 1

Related Questions