jquery text area problem - php

i have one text area
<textarea name="message" id="message" class="text_field" style="height:200px; width:500px"></textarea>
if i type data in the text area like this
hello
this is my test message
bye
'abc'
i use following statement to get data from text area
var message = $('#message').attr('value');
i pass this value to other php file like
var data = {message:message};
$.ajax({
type: "POST",
url: "show.php",
data: data,
etc
when i see data in post value firebug in show exactly i type the data (with new lines & spaces etc)
and in php file
$message=$_POST['message'];
$Content = file_get_contents($temp_page);
$Content = str_replace('%%a%%', $message, $Content);
now when i use
echo $Content
i get all the text in one line not exact i type in text area...
hello this is my test message bye 'abc'
Thanks

That is because browsers don't print newlines outside textareas or other elements that are defined to print the output as is, for example <pre>.
You need to use echo nl2br($Content); to substitute newlines with <br /> elements, which is the equivalent of a newline in (x)HTML.

If you're echoing straight to the browser, try viewing the source of the page. You may find that the browser is interpreting the text as HTML, in which case it will collapse all your whitespace and newlines. Viewing the source should in theory show you what you originally typed, with newlines, extra space etc.

Think about what happens if you just typed in some HTML source like this:
<p>
Hello there.
This is
my
HTML
source, and I hope
you
like it.
</p>
What does that look like? Right — the browser will treat all those newlines as simple whitespace, and collapse it all as it lays out the non-whitespace characters.
When you dump out the textarea contents (by the way: get the value with $('#whatever').val()) you're doing the same thing. If you want to preserve the formatting, well, that can be a headache. You can try putting it in a <pre> block, or you may have to explicitly find the newlines and convert them to <br> elements.
edit: See #Tatu's answer for the php function you want.

IF you right click and click 'View Source' (or some close equivalent), you should see the properly formatted text. This is due to the way that HTML works. Even if you write the following HTML:
hello
this is my test message
bye
'abc'
The output will be inline. You have to write after each line, or wrap the text in a <pre> tag.

Maybe you can try:
nl2br($Content);

Related

PHP output string and maintain spacing [duplicate]

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

New line ("\n") in PHP is not working

For some strange reason, inserting echo "\n"; and other scape sequence characters are not working for me, that's why I am just using <br /> instead.
The images of the results of examples in books and other documentations seems just alright. I'm currently using XAMPP and already used WAMPP with the same actual result. Why is that?
Edit:
It seems that I cannot understand the concept after comparing your answers with this:
PHP Linefeeds (\n) Not Working
Edit:
Sorry I didn't realized that the link above is referring to a php code writing to a file. I just wonder why I have these few php sample programs that uses \n even though it outputs in a webpage. Thanks everyone.
When you run a PHP script in a browser, it will be rendered as HTML by default. If the books you’re using show otherwise, then either the code or the illustration is inaccurate. You can use “view source” to view what was sent to the browser and you’ll see that your line feeds are present.
<?php
echo "Line 1\nLine 2";
?>
This will render in your browser as:
Line 1 Line 2
If you need to send plain text to your browser, you can use something like:
<?php
header('Content-type: text/plain');
echo "Line 1\nLine 2";
?>
This will output:
Line 1
Line 2
PHP Linefeeds (\n) Not Working is referring to sending output to a file rather than the browser.
You should be looking for nl2br(). This will add line breaks (<br>) to your output which will be rendered by the browser; newlines are not.
The echo "\n" is probably working, just not the way you expect it to.
That command will insert a new line character. From the sounds of it, you're using a browser to view your output. Note that if you wrote an HTML file that had a body contents that looked like:
<p>This
is
a
test </p>
The browser rendering would not include the new lines, and would instead just show "This is a test"
If you want to see the newlines, you could view source, and you'll see that the source code includes the new lines.
The rule of thumb is that if you need new lines in a browser, you need to use HTML (e.g. <br />), while if you want it in plain text, you can use the \n
<br /> is the HTML Tag for new line, whereas
"\n" is to output a new line (for real).
The browser doesn't output a new line each time the HTML file goes to the next line.
You can use the nl2br function to convert \n to <br>
As said before, HTML does not render \n as new line. It only recognizes the <br> html tag
If you are working with HTML (viewing the result in browser for example) you have to use the HTML way of linebreaks which is: <br>
/n only works if it is used as a simple text but here as we code in a html doc it takes it as a HTML text hence you can use </br> tag instead.
PHP outputs on the browser and browser only render output in HTML, any other output format will be ignored by the browser.
If you want browser to keep your standard output format as it is, you should enclose your output under HTML's <pre> tag. It preserves the formatting:
echo "<pre>";
echo "This is first line\nThis is new line";
echo "</pre>";
This will be rendered as
This is first line
This is new line
Alternatively, you can mention content type to be plain text in the header:
header('Content-type: text/plain');
echo "This is first line\nThis is new line";
This will tell the browser to render the output as plain text. And the browser will encolse the output automatically in <pre> tag.
solution is echo nl2br or=> <br>

Textarea accepts new line but in span tag it won't display properly

I'm having a problem on displaying values that were from a textarea.
I wanted users to enter some text in my textarea at the same time they may enter a new line(they will hit enter) then after that one, I want to get the value of that textarea and display it in my <span> tag.
The problem here is that new lines aren't displaying in my span tag, I don't know what happen.
For example a user has type something like this in in my text area:
Hello World!
How Are you?!
The problem is that in my span tag, it displays without new line, so the outcome is like this:
Hello World! How Are you?!
I don't want to use some WYSIWYG application just to allow new lines in my span. I believe there's a way for this one.
Please guide me on this one. Your help wouldbe greatly appreciated!
Thanks! :)
Textarea's make \n's, what you want is the html equivalent <br /> so use this string replace to replace all the \n's to <br />'s and you're golden.
In javascript it would be
str = str.replace(/\n/g, '<br />');
In php it would be
$str = nl2br($str);
It is normal for the browser to compress multiple white-space characters and newline characters back to a single space. Inserting <br> elements where you want line breaks is the usual method.
Your question is tagged with both PHP and JavaScript, but you don't need both for this task. If you're displaying data from a database then it'd be best to do the processing in PHP code. For dynamic page updates in the browser you'd use JS.
In PHP use nl2br() when preparing the text that ends up in the span:
echo nl2br($strWithNewLines);
Or in JS:
var stringWithBrs = strWithNewlines.replace(/\n/g, '<br />')
Try this trick.
<pre><span id="wrapContent">
<?php echo $str; ?>
</span></pre>

Want to get <br> tags from textarea, though no other HTML at all

I'm doing a web builder and I'm having a bit of trouble.
I want to have a textarea that you can enter text that you want to your TEXT element. This element I disabled the HTML when it´s later off previewed by simply putting a .innerText before posting preview. Though, I might just need the <br> tag.
I can use PHP or JS. Any ideas?
Use nl2br(htmlspecialchars($str)) when displaying the text. Note that the order of the function calls matters - first you escape all HTML in the string and then you convert linebreaks to HTML linebreaks.
When you are typing in a textarea, and the return key is pressed,
What actually goes on behind the scenes is this
Hi, \n There
which produces the following in the textarea.
Hi
There
Hence, what you would need to do is essentially change the \n (newline) to break tags.
http://php.net/manual/en/function.nl2br.php
Just use a plain textarea, afterword use a function like nl2br when you display it in your html page
nl2br(htmlspecialchars($string))
changes the line breaks to <br /> tags, so you can display them in html as seen in the textarea.

Why does PHP echo'd text lose its formatting?

Any ideas why formatted text from DB, when echo-ed out in php loses its formatting, i.e. no new lines? Thanks!
Use nl2br().
New lines are ignored by browser. That's why you see all text without line breaks. nl2br() converts new lines to <br /> tags that are displayed as new lines in browsers.
If you want to display your text in <textarea>, you don't need to convert all new lines to <br />. Anyway, if you do it... you will see "<br />"s as text in new lines places.
Because there are no html tags for formatting!
Try the nl2br function.
You could try add nl2br() function...
something like this: echo nl2br($your_text_variable);
It should work ;-)
The reason
This is the default behavior for all user agents. If you look at the page source, you'll see that your text has the same formatting like the one in the database (or textarea).
The reason of your confusion is probably that you once see the text in the <textarea> tag, which displays preformatted text, does not interpret the tags, and in the other case the text is interpreted (whitespace is not important in this case).
The browsers don't display new lines, unless specifically asked for - using <br> tag or any block level tags.
No tags == no new lines.
The fix
If you store preformatted text in the database,
you should wrap the output in the <pre> tag.
You may want to convert the formatting characters to the HTML tags you need using set of functions like nl2br, str_replace etc.
You may also correct your structure to store the HTML in the database instead of just plain text (however markup looks like a better solution).
See similar question:
How do I keep whitespace formatting using PHP/HTML?
The difference between the two images you show is that one has the text in a <textarea></textarea> and the other does not ... if you want 1:1: <textarea><?php echo $yourVariable;?></textarea>
It does output what you say to output. If the text is pre-formatted, put it inside the HTML <pre></pre> tag in your output script.
This should be helpful in answering.
How do I keep whitespace formatting using PHP/HTML?enter link description here
Set up a string preprocessing code for both input to database and output to display page

Categories