Reputation: 339
I have a text field in my form which asks the user to enter his/her info. I want that when i submit the form, the entered data has all line breaks replaced with a <br />
tag so that when this data is stored in the database and displayed later then the formatting is correct.
<form action="index.php" method="post" name="add" enctype="multipart/form-data">
<p id="bio" style="display:none;"><label class="left">Bio</label><textarea name="bio" rows="10" cols="50" class="contact"></textarea></p>
<p class="submit" style="text-align:center;"><input type="submit" name="add" value="Submit" /></p>
</form>
I know that i have to use str_replace, but I just don't know where to use it. I think I'm getting confused as to when the entered information will be formatted.
Upvotes: 2
Views: 1230
Reputation: 20899
You should not alter data, when saving it (beside escaping of unwanted chars, and date-conversions to your desired format (UTC or else)). Instead alter it, when displaying it.
So, save it in the database with the \n
s (allowing to easily edit it later on) and when displaying the data, simple use nl2br()
around the variable, holding the text from the database.
Otherwise - upon edit - you would need to convert <br>
back to \n
s and again to <br>
s upon save.
Upvotes: 10