dole doug
dole doug

Reputation: 36048

Using PHP to save from textarea to db

I have a post form with a text area in it. When I save the text from my textarea into mysql db, the text is saved with some white spaces before and after my actual test.

Why is happening this? How can I overcome this?

Thanks in advance

Upvotes: 0

Views: 986

Answers (3)

GordonM
GordonM

Reputation: 31730

There's probably whitespace in your markup. For example:

<textarea>
    <?php echo ($textareavalue); ?>
</textarea>

You could either remove the whitespace

<textarea><?php echo ($textareavalue); ?></textarea>

Or you could trim() the input before storing it to the database

$_POST ['textareavalue'] = trim ($_POST ['textareavalue']);

Upvotes: 3

Rukmi Patel
Rukmi Patel

Reputation: 2561

you can use trim function before inserting into database for perticular that post data...

$text_area = trim($_POST['text_area']);

it will remove spaces from begining and end of the string...

Upvotes: 0

Abhi Beckert
Abhi Beckert

Reputation: 33349

If you have code like this:

<textarea name="foobar">
  <? echo $contents; ?>
</textarea>

Then you are adding whitespace to the value before/after the <? ... ?> tags (note, php does try to remove whitespace in some situations, so sometimes you can get away with it).

The fix is to do this:

<textarea name="foobar"><? echo $contents; ?></textarea>

Upvotes: 0

Related Questions