ISSET() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • major

    #1

    ISSET() question

    The following code processes a blank field as though it has some value
    and proceeds as though it was set to some value.

    Apparently isset() is not working, because it thinks that a blank text
    field is set to something.

    Please let me know how to use isset() correctly.

    Thanks ahead.

    <?php
    if(!isset($_POS T['varA'])||!isset($_POS T['varB'])){
    ?>
    <form action="isset.p hp" method="POST">
    <table><tr>
    <td>Value 1:</td><td><input type="text" name="varA" value=""></td></
    tr>
    <tr><td>Value 2:</td><td><input type="text" name="varB" value=""></
    td></tr>
    <tr><td colspan="2"><in put type="submit" value="submit"> </td></tr>
    </table>
    </form>
    <?php
    }

    else {
    $x=$_POST['varA'];
    $y=$_POST['varB'];

    if($x>$y){
    echo "Value 1:".$x." is larger";
    }else

    echo "Value 2:".$y." is larger";
    }

    ?>
  • Guillaume

    #2
    Re: ISSET() question

    major a écrit :
    Apparently isset() is not working, because it thinks that a blank text
    field is set to something.
    Exactly, "blank" is still a value. isset just verify that the variable
    *is set*, whatever its content.

    Regards,
    --
    Guillaume

    Comment

    • mail.cedric.chevalier@gmail.com

      #3
      Re: ISSET() question

      On 28 fév, 08:51, major <extreme...@gma il.comwrote:
      The following code processes a blank field as though it has some value
      and proceeds as though it was set to some value.
      >
      Apparently isset() is not working, because it thinks that a blank text
      field is set to something.
      >
      Please let me know how to use isset() correctly.
      >
      Thanks ahead.
      >
      <?php
      if(!isset($_POS T['varA'])||!isset($_POS T['varB'])){
      ?>
      <form action="isset.p hp" method="POST">
      <table><tr>
      <td>Value 1:</td><td><input type="text" name="varA" value=""></td></
      tr>
      <tr><td>Value 2:</td><td><input type="text" name="varB" value=""></
      td></tr>
      <tr><td colspan="2"><in put type="submit" value="submit"> </td></tr>
      </table>
      </form>
      <?php
      >
      }
      >
      else {
      $x=$_POST['varA'];
      $y=$_POST['varB'];
      >
      if($x>$y){
      echo "Value 1:".$x." is larger";
      >
      }else
      >
      echo "Value 2:".$y." is larger";
      >
      }
      >
      ?>
      use empty() :

      if ( ( isset($_POST['varA'] && !empty($_POST['varA']) ) || ...

      ichevc

      Comment

      • Mason Barge

        #4
        Re: ISSET() question


        "major" <extremerep@gma il.comwrote in message
        news:b2bd51d2-34e5-472a-868b-55681f053d7b@p4 3g2000hsc.googl egroups.com...
        The following code processes a blank field as though it has some value
        and proceeds as though it was set to some value.
        >
        Apparently isset() is not working, because it thinks that a blank text
        field is set to something.
        >
        Please let me know how to use isset() correctly.
        This one, I actually can help with :)

        This page will save you a lot of grief if you take a few minutes to read and
        understand it:


        Comment

        • Tony

          #5
          Re: ISSET() question

          major wrote:
          The following code processes a blank field as though it has some value
          and proceeds as though it was set to some value.
          >
          Apparently isset() is not working, because it thinks that a blank text
          field is set to something.
          Yes, if the value of a field is "", then it is still set. You would want
          to test for both:

          if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...

          Or, if you want to count " " as blank:


          if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...

          There may be a more efficient way to do this - anyone know of one?

          Comment

          • Norman Peelman

            #6
            Re: ISSET() question

            Tony wrote:
            major wrote:
            >The following code processes a blank field as though it has some value
            >and proceeds as though it was set to some value.
            >>
            >Apparently isset() is not working, because it thinks that a blank text
            >field is set to something.
            >
            Yes, if the value of a field is "", then it is still set. You would want
            to test for both:
            >
            if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...
            >
            Or, if you want to count " " as blank:
            >
            >
            if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...
            >
            There may be a more efficient way to do this - anyone know of one?
            Yes, validate it with a regex also:

            $name = (isset($_POST['name']) &&
            eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;

            if (!$name)
            {
            ....
            }

            would check to see if the post variable has been set and if it matches a
            string of alpha a-z (upper & lower case), and is at least 2 characters
            but not more than 25 characters in length. If not it's set to false and
            you take appropriate action.

            --
            Norman
            Registered Linux user #461062

            Comment

            • Tony

              #7
              Re: ISSET() question

              Norman Peelman wrote:
              Tony wrote:
              >major wrote:
              >>The following code processes a blank field as though it has some value
              >>and proceeds as though it was set to some value.
              >>>
              >>Apparently isset() is not working, because it thinks that a blank text
              >>field is set to something.
              >>
              >Yes, if the value of a field is "", then it is still set. You would
              >want to test for both:
              >>
              >if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...
              >>
              >Or, if you want to count " " as blank:
              >>
              >>
              >if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...
              >>
              >There may be a more efficient way to do this - anyone know of one?
              >
              Yes, validate it with a regex also:
              >
              $name = (isset($_POST['name']) &&
              eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;
              >
              if (!$name)
              {
              ....
              }
              >
              would check to see if the post variable has been set and if it matches a
              string of alpha a-z (upper & lower case), and is at least 2 characters
              but not more than 25 characters in length. If not it's set to false and
              you take appropriate action.
              >
              But what if I don't want to validate according to those rules?

              Comment

              • Jerry Stuckle

                #8
                Re: ISSET() question

                Tony wrote:
                Norman Peelman wrote:
                >Tony wrote:
                >>major wrote:
                >>>The following code processes a blank field as though it has some value
                >>>and proceeds as though it was set to some value.
                >>>>
                >>>Apparently isset() is not working, because it thinks that a blank text
                >>>field is set to something.
                >>>
                >>Yes, if the value of a field is "", then it is still set. You would
                >>want to test for both:
                >>>
                >>if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...
                >>>
                >>Or, if you want to count " " as blank:
                >>>
                >>>
                >>if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...
                >>>
                >>There may be a more efficient way to do this - anyone know of one?
                >>
                >Yes, validate it with a regex also:
                >>
                >$name = (isset($_POST['name']) &&
                >eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;
                >>
                >if (!$name)
                >{
                > ....
                >}
                >>
                >would check to see if the post variable has been set and if it matches
                >a string of alpha a-z (upper & lower case), and is at least 2
                >characters but not more than 25 characters in length. If not it's set
                >to false and you take appropriate action.
                >>
                >
                But what if I don't want to validate according to those rules?
                >
                Then make up your own rules. And accept that if you don't do it right,
                someone can delete your entire database - or at least an entire table.

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                • Norman Peelman

                  #9
                  Re: ISSET() question

                  Tony wrote:
                  Norman Peelman wrote:
                  >Tony wrote:
                  >>major wrote:
                  >>>The following code processes a blank field as though it has some value
                  >>>and proceeds as though it was set to some value.
                  >>>>
                  >>>Apparently isset() is not working, because it thinks that a blank text
                  >>>field is set to something.
                  >>>
                  >>Yes, if the value of a field is "", then it is still set. You would
                  >>want to test for both:
                  >>>
                  >>if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...
                  >>>
                  >>Or, if you want to count " " as blank:
                  >>>
                  >>>
                  >>if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...
                  >>>
                  >>There may be a more efficient way to do this - anyone know of one?
                  >>
                  >Yes, validate it with a regex also:
                  >>
                  >$name = (isset($_POST['name']) &&
                  >eregi('^[a-zA-Z]${2,25}',$_POST['name'])) ? $_POST['name'] : false;
                  >>
                  >if (!$name)
                  >{
                  > ....
                  >}
                  >>
                  >would check to see if the post variable has been set and if it matches
                  >a string of alpha a-z (upper & lower case), and is at least 2
                  >characters but not more than 25 characters in length. If not it's set
                  >to false and you take appropriate action.
                  >>
                  >
                  But what if I don't want to validate according to those rules?
                  What rules do you want to validate by?

                  --
                  Norman
                  Registered Linux user #461062

                  Comment

                  • Mason Barge

                    #10
                    Re: ISSET() question


                    "Tony" <nospam@example .comwrote in message
                    news:13senkbp28 96814@corp.supe rnews.com...
                    major wrote:
                    >The following code processes a blank field as though it has some value
                    >and proceeds as though it was set to some value.
                    >>
                    >Apparently isset() is not working, because it thinks that a blank text
                    >field is set to something.
                    >
                    Yes, if the value of a field is "", then it is still set. You would want
                    to test for both:
                    >
                    if ( isset($_POST['name']) && ($_POST['name'] != "") ) ...
                    >
                    Or, if you want to count " " as blank:
                    >
                    >
                    if ( isset($_POST['name']) && (trim($_POST['name']) != "") ) ...
                    >
                    There may be a more efficient way to do this - anyone know of one?
                    if($_POST['name']) {

                    This will, however, evaluate 0 and "0" to false. It will also throw a
                    warning. !empty() is exactly the same thing, but does not throw a warning.

                    Comment

                    • good@respnse.sic.com

                      #11
                      Re: ISSET() question

                      In article <13sjj08ge122ic f@corp.supernew s.com>, nospam@example. com
                      says...

                      Otherwise - just how "efficient" do you need a $_POST test to be?
                      Stop worrying - do it and move on. That what script languages like PHP
                      are for.

                      If regex is troubling you or you want to do more validation I'd take a
                      look at www.streamforensics.com to automate your forms validations.
                      Their system makes life a whole lot easier in so many ways.

                      You probably have more pressing problems than the one you are worrying
                      about?
                      >
                      Jeez, people - I was just asking. I don't know everything about PHP, and
                      was simply admitting there might be a better way than mine.
                      >
                      Is the moon full or something? People seem to be awfully touchy lately.
                      >
                      Tony - I wasn't having a go I was just trying to explain why I didn't
                      think it worth worrying about instead of just saying so.

                      Sorry if it read bad to you.

                      Comment

                      Working...