Jump to content

Formatting form data


dbillings

Recommended Posts

I have a problem understanding how I can submit form data to a mysql database and preserve the formatting that the user wants. Essentially the problem is line breaks. When a user inserts data into a text area the text automatically jumps to the next line when the one line fills up with text. Yet when the data is submitted to mysql it doesn't preserve the breaks. I've tried several solutions like the <pre> tag. Basically I want the break that the html textarea creates automatically when it reaches the end of the text area to generate a <br /> tag in my data submission. If anyone can give me a solution I would appreciate it. Thanks.

Link to comment
Share on other sites

asically I want the break that the html textarea creates automatically when it reaches the end of the text area

 

There is no linebreak created automatically, its just the wrapping affect of displaying data within a textarea. You could try using wordwrap when displaying this data back to fake it.

 

Another function to look into would be nl2br, this comes in handy for changing actual linebreaks into

<br />

tags.

 

Note: Both these functions should only be applied to your data on the wway out of the database. Store your data in its raw format wherever possible.

Link to comment
Share on other sites

<pre>Hello                    This                              is                                    a                  test                  of                                  how                      this                        forum                          handles                                        a                                              pre                  tag                              </pre>

Link to comment
Share on other sites

dbillings, basically you have 2 options (yes it always comes down to 2 options it seems)

 

1. Use a WYSIWYG type Editor and save the contents of that to your DB

2. Do as suggested, save the textarea as normal, and then when you do something like this:

 

<?php

$q = "select textarea from myTable where id='1'";
$result = mysql_query($q);
$data = mysql_fetch_array($result);

print nl2br($data['textarea']);
?>

 

 

Link to comment
Share on other sites

TinyMCE.

 

To get the non breaking spaces, feel free to write your own function, something like..

 

<?php
function format_output($out) {
/*
    replaces " " with %nbsp (replace with &)
  */
  return nl2br(str_replace (" ", "%nbsp; ", $out));

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.