I used textarea to upload the user discription into the database of phpmyadmin. User can use any characters and spaces, new line, and other special characters. When I retrive the infomation from the database its showing all in one para with no formatted text. I want the text to appear the same as the user inserted like how its appearing in the forums. Below are the codes I used....
Input html
<textarea class="noticearea" name="notice_area" id="notice_area" required="required"><?php echo htmlspecialchars($_POST["notice_area"]); ?></textarea>
php read
$notice_area = mysql_real_escape_string($_POST["notice_area"]);
SQL insert
$noticeinsert = mysql_query("INSERT INTO notice (notice_area) VALUES ('$notice_area')");
retrive infomration
<?php echo htmlspecialchars($notice_area); ?>
Everything is fine no error or warning. I can retrive easily but not in a format. Please suggest me if i have to add any code.
You might need the following code for displaying.
// $notice_area assumes you have database query and the reuslt is correctly fetched.
echo str_replace("\\r\\n","<br/>", (htmlspecialchars(($notice_area))));
Why?
http://php.net/manual/en/function.mysql-real-escape-string.php
mysql_real_escape_string() will add slashes prior to \n , \r, so on.
after inserting into db then are then \n \r in plain text because you added them by \\n and \\r.
so after getting the plain text of \n and \r, you need to replace them with , then it is it :D
Related
I have a form where users can enter anything, for instance, suppose a user entered:
Hello World!
This is a new line.
This was written after two new lines.
The data that user submits using the form is inserted in DB:
$data = mysqli_real_escape_string($dbc, $_POST['text']);
$sql = "INSERT INTO data (Data) VALUES ('$data')";
$exec = mysqli_query($dbc, $sql);
Now it gets stored in database but when I fetch the text from the DB to show to the user, it displays:
Hello World! This is a new line. This was written after two new lines.
As you can see, the new lines are ignored. I also want to show line breaks.
I tried:
$data = mysqli_real_escape_string($dbc, str_replace('\n', '<br>', $_POST['text']));
but that doesn't work either. How can I show line breaks when displaying data from mysql?
It is best to put your user input into the database unaltered (except for escaping, of course) in case you wish to query against the user input, or change your display behavior later on. That said, upon building your page and displaying the data, use
echo nl2br(htmlspecialchars($row['text'], ENT_QUOTES));
nl2br() converts all the "\r\n" or "\n" to <br /> so that it displays nicely. htmlspecialchars() converts any special characters the user typed into the field originally to proper html escape sequences.
Your code would work, except your \n should be wrapped in double quotes instead of single quotes. Single-quoted strings ignore escape sequences in PHP. However, as shown, a built-in function already exists for accomplishing this.
I believe you want nl2br. http://php.net/manual/en/function.nl2br.php The str_replace won't work because you'd need the \n in double quotes. As is you are searching for a literal '\n'.
$data = mysqli_real_escape_string($dbc, nl2br($_POST['text']));
I have a problem: so I have a data thats coming out of the database and stored in a text area. When the user changes the data in the text area, the content is sent to javascript and via AJAX (POST to a PHP script) the database is updated. This works fine until the user starts adding newlines. Then javascript transforms this into a \n-character and thus it gets stored in the database as \n.
What I want is to have actual newlines in my database and not the \n newline-characters. Is there any way that I can use php to replace the \n with an actual newline (NOT a br)? I have tried altering the database field after the edit with the char(10), but for some reason this is not working in the script except when I do it manually in phpmyadmin?
When editting with a full php request, the newline in a text area is correctly stored as a char(10) in mysql, not as \n.
Anyone got a clue?
Store it as it is but escape first with real_escape_string
real_escape_string converts what is a newline into the 4 character string '\n\r'
$text = $mysqli->real_escape_string($text);
Use [nl2br][1] function to replace /n with newline
Insert line breaks where newlines (\n) occur in the string:
<?php
echo nl2br("One line.\nAnother line.");
?>
The browser output of the code above will be:
One line.
Another line.
I used mysqli_real_escape_string() to insert the data from a html form textarea input into the MySQL table.
The input was like:
It was inserted in table properly.
But when I fetch it and try to echo it, it prints in single line as
Hi H"ow" are you? I 'm f'ine /\ How you doing?
How to display it the way it was inserted?
use nl2br() to convert new lines to <br> then store it.
for more information : http://php.net/manual/en/function.nl2br.php
for whitespaces and indentation use html_entities()
If it also doesnt work then you would have to do it using str_replace() by replacing those charcter with one in html like
I have a text field that inserts its content into an SQL table. Often, I will want this content to have <p> html tags within, based on line breaks in the text field. I have tried doing a replace before inserting:
str_replace("</p><p>", "\n", $_POST["body"]);
and I have tried doing a replace with escape characters:
str_replace("</p><p>", "\n", $_POST["body"]);
with no success. Meaning they still appear as text field line breaks. There is no security issue as the field can only be accessed by an administrator. Thank you for your help.
it seems both times you are trying to replace contrary - a </p><p> to \n which obviously fails
try to swap str_replace argumants.
I don't understand what SQL has to do here though
How about replacing each separately?
str_replace(array("</p>", "<p>"), array("\n","\n"), $_POST["body"]);
I think the problem is in " latter, like google, try replacing it with ' or call function addslashes() on your text.
I am trying to parse this xml document in which a newline is required for certain fields and must be inserted into the database with the newline. But I've been running into problems.
1)First Problem: \n Character
The first problem I had was using the \n like below.
<javascript>jquery_ui.js\nshadowbox_modal.js\nuser_profile.js\ntablesorter.js</javascript>
The problem was in the database the field came out ot be jquery_ui.js\nshadowbox_modal.js\n... and when output into html it was jquery_ui.jsnshadowbox_modal.jsn...............
2) Then I tried actually having newlines in the xml
<javascript>jquery_ui.js
shadowbox_modal.js
user_profile.js
tablesorter.js</javascript>
The problem was the output become %20%20%20%20%20%20%20%20%20%20shadowbox_modal.js, and so forth. So how can I get a newline to hold from xml when entered into a database and then output with the newline still?
Remove the spaces from your second example.
You probably entered a tab and/or spaces for readability, but these get inserted too.
%20 is an urlencoded space.