unwanted spaces & carriage return added during textarea echo

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
errcricket
Forum Newbie
Posts: 3
Joined: Mon Mar 30, 2009 11:30 am

unwanted spaces & carriage return added during textarea echo

Post by errcricket »

i have a form that is checked before user input is added to a database. there is a textarea for comments (this is not added to database) that needs to be echoed if there is a problem with the form submission. when there is a problem, the comments are echoed, but spaces and carriage returns are added. there is a 500 character limit, and these unwanted additions remove characters at the end of the string. i have tried using trim() but to no avail. any thoughts?

Code: Select all

 
<form>...
Please write any comments, questions, or suggestions you may have below (max 500 characters).<br>   
<input onblur="textCounter(this.form.recipients,this,500);" disabled  onfocus="this.blur();" maxlength="3" size="3" name="counter" value="500" > Remaining characters<br>
 
<textarea onblur="textCounter(this,this.form.counter,500);" onkeyup="textCounter(this,this.form.counter,500);" id="comments" rows="5" cols="90" name="comments"> 
<?php 
$comments; 
?> 
</textarea>
...
</form>
 
 
i have tried many things, but as it stands, here is the scrubbing (major exfoliating) section.

Code: Select all

 
/*------- users input textbox -------------------------------------------------*/
    if (empty($_POST['comments']) != TRUE)
    {
        $_POST['comments'] = preg_replace('/[^a-zA-Z0-9\'.\s]/', '', $_POST['comments']);
        $_POST['comments'] = strip_tags($_POST['comments']);
        $_POST['comments'] = stripslashes(htmlspecialchars($_POST['comments']));
        $_POST['comments'] = trim($_POST['comments']);
        $comments = $_POST['comments'];
    } 
 
thank you for any assistance you can provide.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: unwanted spaces & carriage return added during textarea

Post by McInfo »

I suspect that the extra whitespace is coming from the HTML.

There is a newline between <textarea> and <?php. There is also one between ?> and </textarea>, but that is ignored unless it is ?>, space, newline.

Code: Select all

<textarea>
<?php echo $comments; ?>
</textarea>
Try this.

Code: Select all

<textarea><?php
    echo $comments;
?></textarea>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Wed Jun 16, 2010 6:53 pm, edited 1 time in total.
errcricket
Forum Newbie
Posts: 3
Joined: Mon Mar 30, 2009 11:30 am

Re: unwanted spaces & carriage return added during textarea echo

Post by errcricket »

Booya McInfo. problem solved! thank you very much.
Post Reply