...

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



Sherrie
01-08-2007, 01: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:


<SCRIPT LANGUAGE="JavaScript">
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
countfield.value = maxlimit - field.value.length;
}
</script>

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!

koyama
01-08-2007, 02: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>

Sherrie
01-08-2007, 02: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?

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


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

Sherrie
01-08-2007, 11:10 PM
koyama,

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?

eg.

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

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

Thanks again!

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

Try


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

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

Sherrie
01-08-2007, 11: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!

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

Sherrie
01-09-2007, 12:38 AM
marek_mar

Thanks for your reply! That worked beautifully!

Greatly appreciate all the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum