visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: Insert carriage returns in mysql database


By: jmsloan U.S.A.  Date: 02/10/2003 00:00:00  English  Points: 50 Status: Answered
Quality : Excellent
Hello everyone I have a <textarea> field on my form. It is set to 75 columns. When they hit send I want the text in the text area to be entered into the database exactly how it showed up in the form. ie with cariage returns and all. I have the textarea wrap set to hard.

Right now when I query to the database it will only have carriage returns if when I'm typing and come to the end of the line and hit enter, but if I let the wrap go it will not give me a carriage return.

Here is my current display
**form to send to database**
<tr>
<td><b>Message:</td>
<td>&nbsp</td>
<td>&nbsp</td>
</tr>
<tr>
<pre><td colspan=3><TEXTAREA NAME=\"message\" COLS=75 ROWS=15 wrap=physical>$message</TEXTAREA></pre></td>
</tr>

Here is my current display

.....
while ($row = mysql_fetch_array($result)){
$send_dt = $row['send_dt'];
$sender = $row['sender'];
$subject = $row['subject'];
$message = $row['message'];

$display_block = "<tr>
<td>&nbsp</td>
</tr>
<tr>
<td><b>From:</b></td>
<td>$sender</td>
</tr>
<tr>
<td><b>Subject:</b></td>
<td>$subject</td>
</tr>
<tr>
<td colspan=2><pre>$message</pre></td>
</tr>
}


Can someone tell me why this won't display like it was typed in the textarea?


Thank,

Jeremy
By: sumotimor Date: 02/10/2003 01:00:00 English  Type : Comment
the HTML textarea does not insert line breaks to wrap the text, it simply wraps the lines without altering the content. This makes sense when you consider a variable-width textarea. If the textarea changes size, you wouldn't want it to alter the content. You need to actually enter the line breaks by hitting the 'return' key.

Maybe your best bet is to make the textarea wide enough that people realize they need to enter line breaks (like here on Experts Exchange)
By: us111 Date: 02/10/2003 01:23:00 English  Type : Comment
$message = nl2br($row['message']);

By: VGR Date: 02/10/2003 01:25:00 English  Type : Answer
I suggest you "cut" the lines yourself when processing the form, because the wrap= doesn't work (even the "hard" one 8-)

relatively easy, a simple loop separating a string in two on word coundaries when $max_row_length (you set it) is reached, etc

then don't forget to nl2br() before writing to the DB ; it'll be easier, and reading back won't pose any problem. At worst, you'll have the html_decode() or the like with entities() also, etc

regards
By: sumotimor Date: 02/10/2003 01:26:00 English  Type : Comment
us111, I think the problem is that there are no nls to convert 2 brs.

jmsloan, one other option you may be interested in is calling wordwrap() on the user's input, which will insert newline chars every x number of characters. This is probably what you're looking for.

<A HREF="http://php.net/wordwrap">http://php.net/wordwrap</a>
By: sumotimor Date: 02/10/2003 01:29:00 English  Type : Comment
Personally, I'd rather save newlines to the database, and call nl2br when I get the data out of the database. HTML Formatting tags don't really belong in the database. What if you decide that 2 sequential line breaks should be a <p> tag down the line? What if you want to use this data for a non-html application? It's easy enough to call nl2br() on data that you fetch from the DB, but it's more of a hassle getting rid of the <br /> tags once they've been added.
By: VGR Date: 02/10/2003 02:44:00 English  Type : Comment
no. This has to be planned beforehand, that's all :D
By: jmsloan Date: 02/10/2003 02:53:00 English  Type : Comment
It's working now. Not sure why it started working. I changed the wrap from physical to hard and made sure I had my <pre> tags and it working now.
By: VGR Date: 02/10/2003 02:58:00 English  Type : Comment
lucky you 8-)
By: sumotimor Date: 02/10/2003 04:25:00 English  Type : Comment
Using wrap="hard" is a very browser-specific fix, and will almost definitely not work on many browsers. The safest solution which also gives you the most control is to use wordwrap() on the server-side to add the line breaks.

Actually, little bit of trivia: the "wrap" attribute isn't even a standard part of HTML, it's a Netscape invention that was never adopted into the HTML spec.
By: VGR Date: 02/10/2003 04:39:00 English  Type : Comment
that's not at all what says this reference I use sometimes :

Form Text Area


<TEXTAREA NAME=name COLS=# columns ROWS=# rows> content </TEXTAREA>
<TEXTAREA NAME=name COLS=# columns ROWS=# rows WRAP=type> content </TEXTAREA>



The text area tag specifies a multiple line text area field within the form that contains it. The NAME attribute is a required field and is used to identify the data for the field. The COLS attribute specifies the width in characters of the text area. The ROWS attribute specifies the number of lines the text area contains. The content is used as an initial value for the field. The field can be scrolled beyond the COLS and ROWS size to allow for larger amounts of text to be entered. The wrap attribute can have values of OFF, SOFT and HARD.

The 3.0 specification adds three new attributes: DISABLED, ERROR, and ALIGN. The DISABLED attribute shows the field but will not allow the user to modify it. The ERROR attribute is used to supply an error message for the field. The ALIGN attribute is used to position the field and can be one of TOP, BOTTOM, MIDDLE, LEFT or RIGHT.

<A HREF="http://www.willcam.com/cmat/html/forms.html#Form%20Text%20Area">http://www.willcam.com/cmat/html/forms.html#Form%20Text%20Area</a>

So the WRAP attribute seems supported by the RFC since HTML 2.0... or else you ould have the "3.0", "3.2" or a vendor's logo in front of the attrivute's description... Look at the "target=" attribute for the FORM element, for instance...
By: sumotimor Date: 02/10/2003 17:19:00 English  Type : Comment
My rebuttal: <A HREF="http://www.w3.org/TR/1998/REC-html40-19980424/interact/forms.html#h-17.7">http://www.w3.org/TR/1998/REC-html40-19980424/interact/forms.html#h-17.7</a>
By: VGR Date: 02/10/2003 17:52:00 English  Type : Comment
indeed

one of the two makes a mistake :D

my rebuttal of the rebuttal :

<A HREF="http://lists.w3.org/Archives/Public/www-html/1996Nov/0076.html">http://lists.w3.org/Archives/Public/www-html/1996Nov/0076.html</a>

It was at the time of the HTML 3.2 review process...


read carefully : "A few months ago, the WRAP attribute was an HTML extension
(I've forgotten its origin), but it was just popular enough that a
number of HTML 2.0-centric books mentioned it. One book in particular
is "HTML: The Definitive Guide" (Musciano/Kennedy, O'Reilly, 1996)"

read also the answer by Carl Morris

and later in the thread :

"You haven't been paying attention. It *IS* supported in MSIE and Netscape.
Right now. Has been for at least two versions of both programs (three for
Netscape). More than 92% of the installed base of browsers supports it. We
*are not* discussing something proposed for future expirmentation but
something nearly universally deployed today." (Tue, 12 Nov 1996)

"it should be formally added to the DTD"

and also : "WRAP is included in HTML Pro, with the values ON,OFF,HARD,SOFT"

By: VGR Date: 02/10/2003 17:57:00 English  Type : Comment
this can also be of interest

<A HREF="http://www.htmlref.com/reference/AppA/textarea.htm">http://www.htmlref.com/reference/AppA/textarea.htm</a>

Do register to be able to answer

EContact
browser fav
page generated in 372.844930 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page