Loading multiline text from database to TextArea - php

I have some multi line text saved in MySql database (VARCHAR 255). When i load it, and process it using standard php function "nl2br", it echoes fine (multi line). 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! What's wrong?
echo "<SCRIPT>FillElements('".$subject."','".$text."');</SCRIPT>";
P.S.
FillElements function:
function FillElements(Sub,Txt)
{
document.getElementById('txtSubject').value=Sub;
document.getElementById('txtMessage').value=Txt;
}

textareas don't actually store the contents in an attribute like value in the same manner as input elements. They actually store the contents in in between the <textarea> and </textarea> tags. Meaning that the contents is actually treated as CDATA in the document.
<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. I have set up a simple example here:
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.
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. You also get a load of useful functions that make writing javascript mucxh easier.
[1]: http://jquery.com jQuery

Newline tags (<br />) don't cause actual new lines in <textarea>.
You can pass the "real" newlines (\n) to your <textarea>, though.
I created a fiddle for that.
EDIT: For the updated FillElements code:
$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:
<script>FillElements("foo","foo
bar
baz");<script>
Correct?
In JavaScript, strings cannot span multiple lines...

Related

Textarea content to database

I have this textarea called personalInfos where i fill the infos in following format :
<p><span class="white">1966 - '69</span><br/> text .... </p>
When i submit it to database, it gets saved ok, same format. When i retrieve the code from database to admin textarea it gets filled ok.
My only problem is on front end where i get displayed the code as text not rendered as html code. So basiclly i see it on the page like this :
<p><span class="white">1966 - '69</span><br/>
Most likely you display fetched code parsed processed by htmlentities() or similar function. This is in most cases the way to go to avoid planting i.e. html in comments. So simply stop doing this after fetching (or insert - depends where you do so) and your content will be outputed as literaly HTML and properly processed by web browser.
You should have a look at htmlspecialchars_decode()
Example
$str = '<p><span class="white">1966 - \'69</span><br/> text .... </p>';
echo htmlspecialchars_decode($str);
Also make sure to escape the single quotes as well.

HTML doesn't get rendered, why does it happen?

On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');

Transform html tags from db

I am using a jquery editor.And the content will be saved in the database along with the html tags . That is for example I have typed "aneesh" in the editor and using editor makes the text bold and italics the saved the content to db .
So the content will be saved in the db along with text and html tags
Now i want take the content from db and want show the text in formated ways that is need to transform the html tags that in my the content want to show like this aneesh Is there any way for this please help me
To store and retrieve html you should use htmlspecialchars() - this preserves html tags etc etc when saving and htmlspecialchars_decode() - this converts them back to proper html that can be displayed - when displaying the retrieved content.
Storing using this and calling the decode function when populating the textarea behind the wysiwig editor will/should have kept everything preserved.
Or you can also use htmlspecialchars()
$a=htmlspecialchars($a);
<text>$a</text>
wud help u save and retrieve the html characters
Just select the data out of database (i'm using PDO): and echo/post the result to the document without string formating:
$sql = 'SELECT * FROM table;';
$stmt = $DB->prepare($sql);
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
echo $row['result'];
if you have content with html tags like i-e <b>Hello</b> in the database, and when you return it from the database again, the jquery editor that you are using will automatically remove those tags and will transform your text again and make it i-e bold (if hello in case).
There is no need to do anything but just echo it out.
you need to use javascript for better and easy solution.
get raw html content from db (with php) to show web browser (with javascript): innerHTML or .html() (JQuery)

Post text from a text area

I have a form where a user types paragraphs into a text area and then it takes them to another page after they submit. How can I pass whatever they typed to the page after they submit? The text area might have linebreaks and if I use a query string to pass the data, it gives me an error. This is my current code to pass the field:
<?php
if(isset($_POST['form']))
{
$title = $_POST['title'];
$body = $_POST['body'];
header("SubmitForm.php?title=$title&body=$body");
?>
<html>
...html form...
It doesn't work when the text area has line breaks in it.
I would suggest installing a wysiwyg editor to make this easier for you, but i assume that would add some time for the learning curve.
The simplest tips I can give you is to set a CSS attribute for your textarea: white-space:pre so that when it gets submitted, all line breaks get sent as well.
On your server side, you would need to use the nl2br() function, so that when it gets saved on your DB or wherever you store them, all line breaks are converted to HTML breaks.
For your additional reference, I had a similar question like this last year.
You really shouldn't be putting anything that long in a query string in the first place. Look into using sessions to store data across pages instead.
(This is assuming I understood the question right)
urlencode the data in order to pass it via query string.

Send <p> tags and simple HTML formatting through a form via PHP

so I need the user to write a review about an article or book and send it to a DB via PHP but with some basic HTML formatting.. I mean, I have a form , when the user writes the review, the data is sent but without any kind of formatting, If the user want to write in a new line, the text is sent like plain text, I need to get also those new line breaks and simple stuff.
I know how to use PHP and DB connection, I just need to know how to get those new line breakes and stuff..
Use nl2br
Just before printing on the screen data from DB. It replaces \n (new line) as <br>
I recommend storing the data as plaintext, and adding the formatting on the way out. This way if you want to change the way it is formatted then you don't have to update every row in the database.
you can use nl2br() if you just need to newlines to be formatted, and a search-and-replace for anything else.
Have you considered using an existing 'plain text to markup' solution, like Markdown?
It (and others like it) allow your users to write plaintext reviews that will be sensibly formatted. (like stackoverflow uses!)
The PHP function nl2br() basically takes every new line your user enters via the form and converts the new line code to a <br> tag.
An example of using this would be:
$text = nl2br("This is text \nThis is a new line of text");
This would create the following code in your database:
This is text<br>This is a new line of text
When the user hits enter in the form textarea, PHP will pick this up as \n.

Categories