douqiao8370
2017-04-11 05:43
浏览 162
已采纳

我无法正确地将sql表中的文本回显到输入textarea框中

In my table there is a column Content with text:

<h2>LAGU</h2><p>Pada minggu ke kota.<br />Naik delman.</p><p>Pak kusir.</p>

This Content column collation is latin_swedish_ci and type is Text in the phpMyAdmin table structure.

In my PHP page, after the query - I give it a name $content

HTML:

<?php echo $content ?>

<form action="update.php" method="post" enctype="multipart/form-data">
    <div id="div_content">
    <label id="label_content">Content</label>
    <textarea id="content" name="content" value="<?php echo $content ?>"></textarea>
    </div>
</form>

The content did show correctly in the first echo. What is in header tag, shown as a header. What is in paragraph tag shown as a paragraph.

But the second echo (which is inside the textarea box), it doesn't show at all. I'm expecting that the textarea box will show exactly the same like the text from the php table, so in case there is a typo or something - the user can edit it directly inside the textarea box.

I've tried one apostrophe instead of double apostrophe

<textarea id="content" name="content" value='<?php echo $content; ?>'></textarea>

The textarea box still empty.

I've tried also this for the textarea

<textarea id="content" name="content" value='.<?php echo $content ?>.'></textarea>

and this

<textarea id="content" name="content" value="'.<?php echo $content ?>.'"></textarea>

Still empty.

But after I've tried this:

<textarea id="content" name="content" value=<?php echo $content ?>></textarea>

The text area box show like this:

LAGU</h2><p>Pada minggu ke kota.<br />Naik delman.</p><p>Pak kusir.</p>>

It lost the 'h2' tag in the beginning. And at the end it reads the closing '>' for the <textarea .....

But the weirdest thing is, if in the PhpMyAdmin I change the table content column by typing dar der dor (just plain text without any weird character), the textarea box is empty no matter what trick I did in the php page.

FYI, besides $content - I also have $title. In phpMyAdmin table, the title column text is MAMA. I put in the php page like this:

<input type="text" id="title" name="title" value="<?php echo $title ?>">

And the inputbox show MAMA.

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongsanhu4784 2017-04-11 05:51
    已采纳

    Change this line:

    <textarea id="content" name="content" value="<?php echo $content ?>"></textarea>
    

    to

    <textarea id="content" name="content"><?php echo $content; ?></textarea>
    

    and try again.

    Explanation: texarea don't have value attribute in it. The value you see is the innerHTML of textarea. So what ever you want to print is between <textarea><?php echo $content; ?></textarea>

    已采纳该答案
    打赏 评论
  • dtpf76658 2017-04-11 05:50

    There is no value attribute for the textarea element, the value must be placed inside the textarea element.

    This is should be your solution:

    <?php echo $content ?>
    
    <form action="update.php" method="post" enctype="multipart/form-data">
        <div id="div_content">
            <label id="label_content">Content</label>
            <textarea id="content" name="content"><?php echo $content; ?></textarea>
      </div>
    </form>
    
    打赏 评论

相关推荐 更多相似问题