Page 1 of 1
PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 5:53 pm
by spartan84821
I've seen a lot of examples of forms that submit to a SQL database where the formatting in the form before submission to the database remains after the form is submitted. Is there a way in which I can keep some of the formatting in my text fields (namely line breaks) so I don't have to manually add HTML tags?
I feel really silly asking this since every single forum in existence keeps a lot of the text field's formatting. I even have a forum on my website!
I'm happy to forward you my code if you need it, I'm just not sure right now what snippets you'd need to see. Thanks!
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 6:33 pm
by EverLearning
Are you talking about formating of the text from textarea?
Check out
nl2br() function.
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 6:48 pm
by spartan84821
The ideal of the nl2br() function seems to be exactly what I'm looking for. But I'm not sure that I can implement it into my code. I have an HTML form with text areas which, upon submission, is sent via a PHP script to an SQL database. I mentioned forums earlier, and now seems like a good time to elaborate. For example, in this forum, as I type this reply, if I press Return on my keyboard the ultimate output in the forum will display a line break. Maybe this is actually an HTML question and I simply have to add something to my text box tag to change the input type away from plain text?
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 6:52 pm
by EverLearning
Use nl2br() on data when you echo it.
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 6:59 pm
by spartan84821
That would definitely work, except that when I try to upload data with line breaks to the database, the text cuts off after the first paragraph (before the first line break). Sorry for being difficult....I'm still learning PHP and SQL.
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 7:06 pm
by EverLearning
Before inserting data into the database use
mysql_real_escape_string() on that data.
Google around on input filtering and output escaping.
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Thu Apr 03, 2008 7:37 pm
by spartan84821
Ah, thanks a lot. In fact, a little more tweaking and I think I've got it!
Re: PHP/SQL: Keeping text field formatting upon submission
Posted: Fri Apr 04, 2008 3:09 am
by JayBird