PHP/SQL: Keeping text field formatting upon submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
spartan84821
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 5:35 pm

PHP/SQL: Keeping text field formatting upon submission

Post 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! :crazy:

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!
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: PHP/SQL: Keeping text field formatting upon submission

Post by EverLearning »

Are you talking about formating of the text from textarea?
Check out nl2br() function.
spartan84821
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 5:35 pm

Re: PHP/SQL: Keeping text field formatting upon submission

Post 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?
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: PHP/SQL: Keeping text field formatting upon submission

Post by EverLearning »

Use nl2br() on data when you echo it.
spartan84821
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 5:35 pm

Re: PHP/SQL: Keeping text field formatting upon submission

Post 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.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: PHP/SQL: Keeping text field formatting upon submission

Post by EverLearning »

Before inserting data into the database use mysql_real_escape_string() on that data.

Google around on input filtering and output escaping.
spartan84821
Forum Newbie
Posts: 4
Joined: Thu Apr 03, 2008 5:35 pm

Re: PHP/SQL: Keeping text field formatting upon submission

Post by spartan84821 »

Ah, thanks a lot. In fact, a little more tweaking and I think I've got it!
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Re: PHP/SQL: Keeping text field formatting upon submission

Post by JayBird »

Post Reply