0

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

2
  • 1
    Hey, can you please paste the corresponding code in here? Commented Nov 21, 2011 at 10:04
  • 4
    Let me guess, you have whitespace between <textarea></textarea>? Commented Nov 21, 2011 at 10:04

3 Answers 3

3

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']);
Sign up to request clarification or add additional context in comments.

Comments

0

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>

2 Comments

Not sure why this was downvoted as it's a perfectly valid answer. However, I'd advise against using short tags.
Yeah, I've never used short tags either. But in this case I think it makes the example clearer. 2 up votes and 2 down votes... no idea why!
0

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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.