View Full Version : Echo function not working with Javascript text area

01-08-2007, 12:50 PM
Hi all!

I've got a text area that uses Javascript to control the length of the user input to 255 characters.

The value of that text area is stored in a session variable so that the user can go back to that page to review their input.

However, when the user returns to the page, the text areas are empty.

Could anyone please tell me where I've gone wrong?

Here's the Javascript code:

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
countfield.value = maxlimit - field.value.length;

and here's the HTML and PHP for my text area:

<textarea name="positive" cols="80" rows="3" id="positive" VALUE="<?PHP echo $_SESSION['answer']['positive'];?>" onKeyDown="textCounter(this.form.positive,this.form.remLen,255);" onKeyUp="textCounter(this.form.positive,this.form.remLen,255);"></textarea>
<input readonly type=text name="remLen" size="3" maxlength="3" value="255">

Thanks a lot in advance for your help!

01-08-2007, 01:12 PM
You are using the <textarea> element incorrectly. There is no value attribute. Example:

<textarea rows="10" cols="80">Text goes in here</textarea>

01-08-2007, 01:16 PM
hmmm... i see.

So does that mean that users cannot view what they typed in the text areas? Or is there another way to get around that?

01-08-2007, 01:27 PM
Like this:

<textarea rows="10" cols="80"><?php echo $_SESSION['answer']['positive'];?></textarea>

01-08-2007, 10:10 PM

Thanks a lot! That works! Really appreciate it!

I've run into another problem though,

If the session variable has a ' in it, it's displaying in the text area as \'

How do I get around this?


$_SESSION['answer']['improvements']="Let's try this!";

it's displaying as Let\'s try this!

Thanks again!

Len Whistler
01-08-2007, 10:38 PM
$_SESSION['answer']['improvements']="Let's try this!";


$_SESSION['answer']['improvements']="Let\'s try this!";

Escape the ' with a backslash. I'm not sure if it will work though.

01-08-2007, 10:59 PM
Len Whistler,

Thanks for your quick reply!

The problem is, that the session variable is stored in the value from a form.

Would there be a way to compare the patterns and replace all \' with ' If there is, how would I do that?

Thanks a bunch!

01-08-2007, 11:15 PM
You probably have magic_quotes on.
You can remove the "\"'s with stripslashes() (http://www.php.net/stripslashes).

01-08-2007, 11:38 PM

Thanks for your reply! That worked beautifully!

Greatly appreciate all the help!