Page 1 of 1

unwanted spaces & carriage return added during textarea echo

Posted: Tue Aug 18, 2009 3:09 pm
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.

Re: unwanted spaces & carriage return added during textarea

Posted: Tue Aug 18, 2009 3:28 pm
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.

Re: unwanted spaces & carriage return added during textarea echo

Posted: Tue Aug 18, 2009 3:35 pm
by errcricket
Booya McInfo. problem solved! thank you very much.