Hello,
On a html form I have a text area where user can post comment.
There is an initial value with indications for the user to read, but when he clicks on the field the comment dissapears letting him write his own comment.
This field is enabled to receive a null value, and that is what I want to happen when the user doesn't write on it. But right now if the user don't write on it, it is storing the initial value ¨type here your comment¨ here is the code:[php] <textarea name="coment" id="coment" cols="32" rows="5" value="Search" onfocus="if
(this.value==this.defaultValue) this.value='';">type here your comment
</textarea>[/php]
I have the idea on how to do it, but don't know how to code it correctly
here is my idea:
[php]<?php
if (['coment']=¨Ingresa aquí cualquier detalle
importante para el piloto, como que tipo de equipaje llevas.¨ {['coment]='null'}
?>[/php]Thanks for any help,
Please enclose any code within the proper code tags. See the Posting Guidelines on how to do that.
MODERATOR
There are a few problems going on here:
The first is in the javascript.
- <textarea name="coment" id="coment" cols="32" rows="5" value="Search" onfocus="if
-
(this.value==this.defaultValue) this.value='';">type here your comment
-
</textarea>
You have defined in the HTML that the value="Search". So when the user will cick on the <textarea> the text will not disappear.
You should remove the value attribute, and add a defaultValue attribute. (note that defaultValue would be invalid xHTML, as xHTML requires lowercase, so better to make it default_value).
Example:
- <textarea name="coment" id="coment" cols="32" rows="5" default_value="type here your comment" onfocus="if
-
(this.value==this.default_value) this.value='';">type here your comment
-
</textarea>
The other problem could be in the content encoding.
Your string: Ingresa aquí cualquier detalle
importante para el piloto, como que tipo de equipaje llevas.
contains some characters that require multibye character encoding. PHP has problems with this, since it treats all strings as single bytes, and does not consider encoding, unless you tell it to.
see: http://www.phpwact.org/php/i18n/charsets
So if you got the encoding wrong, then your string comparison would fail.
That would explain why a shorter string worked for you, since it does not have the character
í which requires multibyte encoding.
Another thing that can cause a problem is the editor you used to write your HTML code. They sometimes add a linebreak in between your HTML tags. Or you could have entered it when you pressed enter.
Consider:
<textarea name="coment" id="coment" cols="32" rows="5" value="Search" onfocus="if
(this.value==this.defaultValue) this.value='';">type here your comment
</textarea>
This actually has a linebreak:
So "type here your comment"
is actually "type here your comment\n"
(or "type here your comment\r\n" depending on the system you used.)
So in your PHP code that compares the strings, its good to remove the ending and begining line breaks first using the trim() function.
http://www.php.net/trim/