coding an if condition

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rodrigo21
    New Member
    • Mar 2008
    • 20

    coding an if condition

    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==th is.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
    Last edited by ronverdonk; Mar 15 '08, 12:01 AM. Reason: code tags!!!
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by rodrigo21
    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:

    <textarea name="coment" id="coment" cols="32" rows="5" value="Search" onfocus="if
    (this.value==th is.defaultValue ) this.value='';" >type here your comment
    </textarea>


    I have the idea on how to do it, but don't know how to code it correctly
    here is my idea:

    <?php
    if (['coment']=¨Ingresa aquí cualquier detalle
    importante para el piloto, como que tipo de equipaje llevas.¨ {['coment]='null'}
    ?>


    Thanks for any help,
    [php]
    if($comment == "Ingresa aquí cualquier detalle
    importante para el piloto, como que tipo de equipaje llevas.")
    $comment = null;
    [/php]

    Comment

    • rodrigo21
      New Member
      • Mar 2008
      • 20

      #3
      Originally posted by markusn00b
      [php]
      if($comment == "Ingresa aquí cualquier detalle
      importante para el piloto, como que tipo de equipaje llevas.")
      $comment = null;
      [/php]
      I have coded exactly what you told me but nothing happens, still storing the initial text. Also tried using { } for the if instruction, using "nul", using 'null' ect. but nothing seems to work. Also I tried deliting the word "aquí" from the text to avoid problems generated by the accent (í). but nothing seems to work.

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        The value of your textarea is 'Search'. You specified that yourself in the textarea statement, i.e. [php]value="Search"[/php].Ronald

        Comment

        • rodrigo21
          New Member
          • Mar 2008
          • 20

          #5
          I tried shortening the text in the comment area tu just "ingresa" (no capital letters, no spaces) and I figured out a code that do the job:

          [code=php]
          if($_POST['comentario'] == "ingresa%")
          {
          $_POST['comentario'] = "";
          }
          [/code]



          but when putting a long text in the comment area (as I need to), the if condition doesnt work, or doesent make the match as it should, maybe it is because of the caps and spaces.

          Any solution for making the condition work with a long text?

          Comment

          • arggg
            New Member
            • Mar 2008
            • 91

            #6
            It should not check for case sensitivity unless you use ===

            try

            [code=php]if(fnmatch("ing resa*",$_POST['comentario']))
            {
            $_POST['comentario'] = "";
            }
            [/code]

            Comment

            • TheServant
              Recognized Expert Top Contributor
              • Feb 2008
              • 1168

              #7
              Originally posted by arggg
              It should not check for case sensitivity unless you use ===

              I thought === was only for checking if the type is the same as well, not case sensitivity?

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by TheServant
                I thought === was only for checking if the type is the same as well, not case sensitivity?
                It seems logical that "hello" is not equal to "Hello" - i think that's how php understands it. So, therefore, PHP neither == or === will see them as being equal.

                As TheServant rightly said === checks for the type of the value being passed.

                I.E it checks whether 11(int) is equal to "11"(string ) - because one is a string and the other an interger, they are not equal.

                /ramble.

                Comment

                • bucabay
                  New Member
                  • Apr 2007
                  • 18

                  #9
                  Originally posted by rodrigo21
                  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==th is.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.


                  Code:
                  <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:

                  Code:
                  <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==th is.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/

                  Comment

                  • rodrigo21
                    New Member
                    • Mar 2008
                    • 20

                    #10
                    Originally posted by bucabay
                    There are a few problems going on here:

                    The first is in the javascript.


                    Code:
                    <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.
                    No problems with the disappearing text, it works like it was.
                    Anyway, thanks for the advice for the second problem! (you don't sound like a newbie)

                    Comment

                    Working...