Display PHP query result in textarea - php

I'm having a little problem over here, I'm trying to make a news system with an edit button, it's all going great but I'm having problems with the "textarea", I can display the results on inputs but when I try to display them in a textarea it wont, look:
This code works perfectly:
<input name="txt_02" size="87" maxlength="100" id="txt_Resumen" maxlength="140" value="<?php echo $not_Resumen?>"/>
This wont:
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion" value="<?php echo $not_Contenido ?>">
</textarea>
I tried with $not_Resumen and other ones in the textarea and it doesn't work, the textarea would show up empty without the text, it should be a little mistake I'm making but I can't find it. Thanks.

Just put it within ><, there's no value attribute:
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion"><?php echo htmlspecialchars($not_Contenido);?></textarea>
You should also use htmlspecialchars so that the textarea will not break if $not_Contenido contains </textarea>.
This is sometimes overlooked, but if $not_Contenido contained something like:
</textarea><script src="http://remotedomain.com/evilscript.js"></script>
An attacker can run anything they want, and all your clients will download and run the script on your website. A common attack would be sending cookies to their domain.

Try like
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion">
<?php echo $not_Contenido; ?>
</textarea>
We con't give value to the textbox.

Value is not attribute of textarea so simply place between the tag <textarea>?</textarea>
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion" ><?php echo $not_Contenido ?>
</textarea>

Place your value between opening and closing tags of textarea as like other HTML tags and textarea has no attribute "value"
<textarea name="txt_descripcion" cols="66" rows="10" id="txt_descripcion"><?php echo htmlspecialchars($not_Contenido);?></textarea>

Related

I cannot echo text from sql table into input textarea box correctly

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

Background text in input type text php

I think its a easy question, but i dont know how to search this question in google
I need a background text in a input-text field. Like: Insert Your Name. And when i press in to the field to insert my name, the text disappears
So here's my code now..
<textarea id="dox" name="<?php echo $_SESSION["dox"]; ?>" rows="25" cols="80">hello type here.</textarea><br />
Can anyone help me? Add everything whats needed... thanks alot <3
You need the placeholder attribute:
<textarea id="dox" name="<?php echo $_SESSION["dox"]; ?>" rows="25" cols="80" placeholder="hello type here."></textarea>
For textarea and input fields you can simply use the "placeholder" attribute like this:
<textarea placeholder="hello type here." id="dox" name="<?php echo $_SESSION["dox"]; ?>" rows="25" cols="80"></textarea>
However, this attribute is not compatible with all (mainly old) Browsers.
If you want to reach those browsers you can use a JQuery Plugin like this for example: https://github.com/mathiasbynens/jquery-placeholder
Or use javascript with onfocus and onblur events like this:
<textarea onfocus="if(this.value=='Placeholder text')this.value='';" onblur="if(this.value=='')this.value='Placeholder text';">Placeholder text</textarea>

Echo var from PHP into textarea control

Desired outcome: define string var in PHP and echo it into textarea control as contents.
Problem: Textarea only contains:
</body>
</html>
INDEX.PHP:
<?php
$y = "Bonkers";
?>
<html>
<body>
Here is a textarea control:<br />
<textarea id='myText' rows="30" cols="120" value='<?php echo $y; ?>' />
</body>
</html>
The textarea element doesn't have a value attribute. This is how you should do it:
<textarea id='myText' rows="30" cols="120"><?php echo $y; ?></textarea>
<textarea> doesn't have an HTML value attribute. Therefore, the correct way to set text within a textarea is as follows:
<textarea id="myText" rows="30" cols="120"><?php echo $y; ?></textarea>
You can access it within JavaScript or jQuery, however, as you would a normal input field.
// jQuery example
$("textarea#myText").val();
The reason why your textarea contains the ending body and html tags is due to how <textarea> is closed; it's only meant to be used as <textarea></textarea>, not <textarea />. (Reference)
Do You Know !! You can echo something in textarea by using placeholder attribute also. Code is as follow.
<textarea placeholder="<?php echo $_SESSION['your_name']; ?>"></textarea>

How to keep text inserted in a html <textarea> after a wrong submission?

Suppose I have a form like this:
<form action="page.php" method="post">
Section1: <input name="section1" type="text"></br>
Section2: <input name="section2" type="text"></br>
Text:</br>
<textarea name="post_text" cols="100" rows="20"></textarea>
<input name="submit" type="submit" value="Submit">
</form>
Usually if I wish to save content inserted in a field of the form I would use this statement:
Section1: <input name="section1" type="text" vale="value="<?php echo $_POST['section1']; ?>""></br>
This way if I make a mistake in submission (error control code is not posted) the values inserted will be kept, and there is no need to reinsert them.
However using it in the textarea tag it will not produce the desired result.
Any ideas on how to do?
Thanks in advance!
Don't forget about htmlspecialchars(). This should help: https://developer.mozilla.org/en/HTML/Element/textarea
<textarea name="post_text" cols="100" rows="20"><?php echo htmlspecialchars($_POST['post_text']);?></textarea>
You could use the same approach, but put the echo between the opening and closing <textarea></textarea> tags, as the textarea doesn't have a 'value' (as such) it has textual content:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['textareaContent']; ?></textarea>
Use the $_POST variable like this.
<textarea name="post_text" cols="100" rows="20"><?= isset($_POST['post_text'])?$_POST['post_text']:'' ?></textarea>
the inline conditional checks if the $_POST['post_text'] is set to remove the NOTICE warning
You would put it inside the <textarea> element like so:
<textarea name="post_text" cols="100" rows="20"><?php echo $_POST['post_text']; ?></textarea>
However, calling the $_POST element directly is not best practice. You should rather do something like this:
<textarea name="post_text" cols="100" rows="20">
<?php echo $var = isset($_POST['post_text']) ? $_POST['post_text'] : ''; ?>
</textarea>
This stops an E_NOTICE error from being reported upon the first page-load.

Setting value of a HTML form textarea?

I'm using the following to set the value of a text area..
<?php
$message = $_REQUEST['message'];
?>
<br/><b>Description</b><br/>
<TEXTAREA NAME="message" COLS=40 ROWS=6 value="<?=$message;?>"></TEXTAREA><br/><br/>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
but it doesn’t appear to be working. The value of message is not null. Does anyone have any idea why it's not filling the value?
Textarea has no value. You need to insert your message between the opening and closing tags.
<textarea><?php echo htmlspecialchars($message); ?></textarea>
<textarea name="message" cols="40" rows="6"><?=$message?></textarea>
Note: Make sure $message is properly sanitized and that short_open_tag is enabled. Otherwise, #fabric's accepted answer is a better answer.
Also you can use this method...
`<textarea rows="12" id="mytextarea"><?php echo $bio; ?> </textarea>
<script>
document.getElementById(`mytextarea`).innerHTML="Include the text you want to display";
</script>`

Categories