I have a textarea and users can type in it. I want to store what they type in a MySQL database but I want to output it with the right amount of spaces like HTML's <pre></pre>. How can I save their input in a database without ruining these spaces?
My code so far just looks like:
<form action='index.php' method='POST'>
<textarea id='area'></textarea>
<input type='submit' value='submit'>
<?php
$input=$_POST['input'];
//mysql query goes here ()
It has been a while since I did this, but here's what I remember: the database doesn't remove the spaces. Two things control the spacing: the textarea's wrap command and the way you show the data later when you retrieve it from the database.
For my forms (that do this successfully) I use textarea wrap="soft"
Then when you take it out of the database you can use pre tags to show it or if you are using php you can use nl2br() on the text to change newlines to html break tags.
Related
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.
I'm relatively new to this and am familiar with echo in PHP but what I need is to have the contents of a field in a database to be placed in the page (but not as a written field)
For example
<?php echo $product_description['description']; ?>
the field description has html formating in it already so when I use 'echo' it writes out for example
<p>text on firstline <br> text on next line
And what I want is that this html from this field in the database that already has html formating to simply placed in the php page which would make it look like this
text on firstline text on next line
I assume I just need to use a different command than ECHO but don't know which one.
Try
echo html_entity_decode($product_description["description"]);
If that works, the HTML in your database has been encoded using htmlentities, so you must decode it to write to a page.
strip_tags is what you're after.
Use it like so: echo strip_tags($your database bit to echo here);
PHP docs for strip_tags
I would like for posts returned from my database, from users, to be formatted the same way they originally input them.
Like when I echo out the text from a row of data it comes out like this:
Hello There.
When the user originally formatted it like this:
Hello
There.
Notice the return? How do I achieve this?
Here is my html:
<form method="post" action="share.php">
<textarea name="story"></textarea>
<div id="share-something-bottom">
<div id="share-something-camera">
<img src="images/camera.png"/>
<button type="submit" class="share"
name="share"><p>Share</p></button>
</div>
</div>
</form>
I am using a varchar field in my database table.
Thanks
Store the formatting in the DB and make sure you escape it before storing it.
If you wish to preserve newlines when you echo them on the page, you need to use nl2br().
echo nl2br($string_from_database);
You could also wrap the string in <pre> tag, but that will also affect the used font (by default).
I'm getting data from my textarea with the following code
$about_me=mysql_real_escape_string(nl2br($_POST['about_me']));
which
1. Receives data, using $_POST.
2. nl2br makes brakes so If I echo this code to user he will see if there were new lines.
3. mysql_real_escape_string to secure code from mysql injections before entering it to database.
So if I echo this code everything works fine.
But If I edit it again through textarea, php goes to mysql gets data, puts it to textarea and I see <br> signs...
How can I get rid of them while editing my text again in textarea ?
How can I get rid of them while editing my text again in textarea ?
Stop using nl2br(), of course. It's entirely wrong here.
You use nl2br() when you want to output data that contains linebreaks to HTML, not when you want to store it in the database. Store data unchanged, format it for viewing.
If you output it into a <textarea> you don't need to use it either, since textareas display linebreaks (whereas HTML in general does not). For the textarea you need htmlspecialchars(), but apparently this is already happening - otherwise you would not see literal <br> showing up.
<?php
function br2nl($string){
$return=eregi_replace('<br[[:space:]]*/?'.
'[[:space:]]*>',chr(13).chr(10),$string);
return $return;
}
?>
Use this while getting data from database and before printing into textarea .
http://php.net/manual/en/function.nl2br.php
Check examples on this page
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...