refactor + help

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

    #1

    refactor + help

    There is probably an incredibly simple solution for this problem, but
    I can't seem to figure it out. Hopefully one of you gurus can help.

    I'm was messing around with PHP today and I wrote a tiny app that asks
    for your name and zipcode. If your zipcode is less than 5 characters,
    it asks you too enter a zip code with the proper amount of characters.

    Here's the code:

    <code>
    <?php



    print <<<_HTML_

    <form method='POST' action='$_SERVE R[PHP_SELF]'>
    Name: <input type='text' name='name'><br /><br />
    Zipcode: <input type='text' name='zipcode'> <br /><br />
    <input type='submit' value='Step 2...'>
    </form>
    _HTML_;

    $zipcode = trim($_POST['zipcode']);

    $zip_length = strlen($zipcode );

    print $_POST['zipcode'];

    if ($zip_length != 5){

    print 'invalid amount of characters';

    } else {

    print ' ';

    }

    ?>

    </code>

    I cannot figure out how to have the the 'invalid amount of characters'
    string post only after the info is passed through the form. Instead it
    displays that string on the bottom of the page without any
    intervention. I know the answer is sitting right in front of my face.
    Maybe someone can help?


    Thanks!
  • Dale

    #2
    Re: refactor + help


    "silverDuck " <JonnDailey@gma il.comwrote in message
    news:462c26b1-9956-4481-a327-4b8d1686c3f4@a1 g2000hsb.google groups.com...
    There is probably an incredibly simple solution for this problem, but
    I can't seem to figure it out. Hopefully one of you gurus can help.
    >
    I'm was messing around with PHP today and I wrote a tiny app that asks
    for your name and zipcode. If your zipcode is less than 5 characters,
    it asks you too enter a zip code with the proper amount of characters.
    >
    Here's the code:
    >
    <code>
    <?php
    >
    >
    >
    print <<<_HTML_
    >
    <form method='POST' action='$_SERVE R[PHP_SELF]'>
    Name: <input type='text' name='name'><br /><br />
    Zipcode: <input type='text' name='zipcode'> <br /><br />
    <input type='submit' value='Step 2...'>
    </form>
    _HTML_;
    >
    $zipcode = trim($_POST['zipcode']);
    >
    $zip_length = strlen($zipcode );
    >
    print $_POST['zipcode'];
    >
    if ($zip_length != 5){
    >
    print 'invalid amount of characters';
    >
    } else {
    >
    print ' ';
    >
    }
    >
    ?>
    >
    </code>
    >
    I cannot figure out how to have the the 'invalid amount of characters'
    string post only after the info is passed through the form. Instead it
    displays that string on the bottom of the page without any
    intervention. I know the answer is sitting right in front of my face.
    Maybe someone can help?
    that's because you aren't checking whether or not it was posted in the first
    place.

    if (isset($_POST['zipcode']) && $zipcode != 5)
    {
    echo 'INVALID POSTAL CODE - ZIP + 5 FORMAT';
    }

    also notice the insanity of having an else statement if all you're going to
    do is echo a space to the browser!

    anyway, hope that helps.


    Comment

    • Jerry Stuckle

      #3
      Re: refactor + help

      silverDuck wrote:
      There is probably an incredibly simple solution for this problem, but
      I can't seem to figure it out. Hopefully one of you gurus can help.
      >
      I'm was messing around with PHP today and I wrote a tiny app that asks
      for your name and zipcode. If your zipcode is less than 5 characters,
      it asks you too enter a zip code with the proper amount of characters.
      >
      Here's the code:
      >
      <code>
      <?php
      >
      >
      >
      print <<<_HTML_
      >
      <form method='POST' action='$_SERVE R[PHP_SELF]'>
      Name: <input type='text' name='name'><br /><br />
      Zipcode: <input type='text' name='zipcode'> <br /><br />
      <input type='submit' value='Step 2...'>
      </form>
      _HTML_;
      >
      $zipcode = trim($_POST['zipcode']);
      >
      $zip_length = strlen($zipcode );
      >
      print $_POST['zipcode'];
      >
      if ($zip_length != 5){
      >
      print 'invalid amount of characters';
      >
      } else {
      >
      print ' ';
      >
      }
      >
      ?>
      >
      </code>
      >
      I cannot figure out how to have the the 'invalid amount of characters'
      string post only after the info is passed through the form. Instead it
      displays that string on the bottom of the page without any
      intervention. I know the answer is sitting right in front of my face.
      Maybe someone can help?
      >
      >
      Thanks!
      First of all, remember that all of the PHP code on the page runs BEFORE
      the page is sent to the client. So your code will run the first time,
      giving the output you see.

      To correct your problem, check to see that the form actually was
      submitted, i.e.

      <?php
      if (isset($_POST['submit'] && post['submit'] == 'Step 2')
      $zipcode = trim($_POST['zipcode']);
      $zip_length = strlen($zipcode );
      print $_POST['zipcode'];
      if ($zip_length != 5){
      print 'invalid amount of characters';
      } else {
      print ' ';
      }


      Also, I recommend you move the test earlier in the file, and if you
      detect an error, put an error message out right at the field, i.e. just
      before the zipcode field. That way the message will be put out just
      before error.

      Alternatively, put the error message(s) at the beginning of the file
      (after any header you might have).

      People look for errors at the error location or the top of the window
      not at the bottom of the window.

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

      Comment

      • silverDuck

        #4
        Re: refactor + help

        On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
        silverDuck wrote:
        There is probably an incredibly simple solution for this problem, but
        I can't seem to figure it out. Hopefully one of you gurus can help.
        >
        I'm was messing around with PHP today and I wrote a tiny app that asks
        for your name and zipcode. If your zipcode is less than 5 characters,
        it asks you too enter a zip code with the proper amount of characters.
        >
        Here's the code:
        >
        <code>
        <?php
        >
        print <<<_HTML_
        >
        <form method='POST' action='$_SERVE R[PHP_SELF]'>
        Name: <input type='text' name='name'><br /><br />
        Zipcode: <input type='text' name='zipcode'> <br /><br />
        <input type='submit' value='Step 2...'>
        </form>
        _HTML_;
        >
        $zipcode = trim($_POST['zipcode']);
        >
        $zip_length = strlen($zipcode );
        >
        print $_POST['zipcode'];
        >
        if ($zip_length != 5){
        >
        print 'invalid amount of characters';
        >
        } else {
        >
        print ' ';
        >
        }
        >
        ?>
        >
        </code>
        >
        I cannot figure out how to have the the 'invalid amount of characters'
        string post only after the info is passed through the form. Instead it
        displays that string on the bottom of the page without any
        intervention. I know the answer is sitting right in front of my face.
        Maybe someone can help?
        >
        Thanks!
        >
        First of all, remember that all of the PHP code on the page runs BEFORE
        the page is sent to the client.  So your code will run the first time,
        giving the output you see.
        >
        To correct your problem, check to see that the form actually was
        submitted, i.e.
        >
        <?php
           if (isset($_POST['submit'] && post['submit'] == 'Step 2')
             $zipcode = trim($_POST['zipcode']);
             $zip_length = strlen($zipcode );
             print $_POST['zipcode'];
             if ($zip_length != 5){
               print 'invalid amount of characters';
             } else {
               print ' ';
        >
        }
        >
        Also, I recommend you move the test earlier in the file, and if you
        detect an error, put an error message out right at the field, i.e. just
        before the zipcode field.  That way the message will be put out just
        before error.
        >
        Alternatively, put the error message(s) at the beginning of the file
        (after any header you might have).
        >
        People look for errors at the error location or the top of the window
        not at the bottom of the window.
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstuck...@attgl obal.net
        =============== ===
        Thank you so much! Great help.

        Comment

        • Jeff

          #5
          Re: refactor + help

          silverDuck wrote:
          On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
          >silverDuck wrote:
          >>There is probably an incredibly simple solution for this problem, but
          >>I can't seem to figure it out. Hopefully one of you gurus can help.
          >>I'm was messing around with PHP today and I wrote a tiny app that asks
          >>for your name and zipcode. If your zipcode is less than 5 characters,
          >>it asks you too enter a zip code with the proper amount of characters.
          >>Here's the code:
          >><code>
          >><?php
          >>print <<<_HTML_
          >><form method='POST' action='$_SERVE R[PHP_SELF]'>
          >>Name: <input type='text' name='name'><br /><br />
          >>Zipcode: <input type='text' name='zipcode'> <br /><br />
          >><input type='submit' value='Step 2...'>
          >></form>
          >>_HTML_;
          >>$zipcode = trim($_POST['zipcode']);
          >>$zip_length = strlen($zipcode );
          >>print $_POST['zipcode'];
          >>if ($zip_length != 5){
          >>print 'invalid amount of characters';
          >>} else {
          >>print ' ';
          >>}
          >>?>
          >></code>
          >>I cannot figure out how to have the the 'invalid amount of characters'
          >>string post only after the info is passed through the form. Instead it
          >>displays that string on the bottom of the page without any
          >>interventio n. I know the answer is sitting right in front of my face.
          >>Maybe someone can help?
          >>Thanks!
          >First of all, remember that all of the PHP code on the page runs BEFORE
          >the page is sent to the client. So your code will run the first time,
          >giving the output you see.
          >>
          >To correct your problem, check to see that the form actually was
          >submitted, i.e.
          >>
          ><?php
          > if (isset($_POST['submit'] && post['submit'] == 'Step 2')
          > $zipcode = trim($_POST['zipcode']);
          > $zip_length = strlen($zipcode );
          > print $_POST['zipcode'];
          > if ($zip_length != 5){
          > print 'invalid amount of characters';
          > } else {
          > print ' ';
          >>
          >}
          >>
          >Also, I recommend you move the test earlier in the file, and if you
          >detect an error, put an error message out right at the field, i.e. just
          >before the zipcode field. That way the message will be put out just
          >before error.
          >>
          >Alternativel y, put the error message(s) at the beginning of the file
          >(after any header you might have).
          >>
          >People look for errors at the error location or the top of the window
          >not at the bottom of the window.
          >>
          >--
          >============== ====
          >Remove the "x" from my email address
          >Jerry Stuckle
          >JDS Computer Training Corp.
          >jstuck...@attg lobal.net
          >============== ====
          >
          Thank you so much! Great help.
          Just remember that doing such checks is typically counter productive.
          You put in a zipcode check and later you want to include Canada. Oops!

          This applies to a whole host of checking that attempts to outsmart the
          client.

          If you have to do this you should consider doing this client side,
          before taking them on a trip to the server and back.

          Jeff

          Comment

          • Geoff Berrow

            #6
            Re: refactor + help

            Message-ID: <S-qdnQmb1tiOjwnVn Z2dnUVZ_sWdnZ2d @earthlink.comf rom Jeff
            contained the following:
            Just remember that doing such checks is typically counter productive.
            >You put in a zipcode check and later you want to include Canada. Oops!
            >
            This applies to a whole host of checking that attempts to outsmart the
            >client.
            Heh, a client of mine wanted to make post codes mandatory until I
            pointed out that they didn't use them in Ireland...

            --
            Geoff Berrow 011000100110110 0010000000110
            001101101011011 001000110111101 100111001011
            100110001101101 111001011100111 010101101011
            The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

            Comment

            • Jerry Stuckle

              #7
              Re: refactor + help

              Jeff wrote:
              silverDuck wrote:
              >On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
              >>silverDuck wrote:
              >>>There is probably an incredibly simple solution for this problem, but
              >>>I can't seem to figure it out. Hopefully one of you gurus can help.
              >>>I'm was messing around with PHP today and I wrote a tiny app that asks
              >>>for your name and zipcode. If your zipcode is less than 5 characters,
              >>>it asks you too enter a zip code with the proper amount of characters.
              >>>Here's the code:
              >>><code>
              >>><?php
              >>>print <<<_HTML_
              >>><form method='POST' action='$_SERVE R[PHP_SELF]'>
              >>>Name: <input type='text' name='name'><br /><br />
              >>>Zipcode: <input type='text' name='zipcode'> <br /><br />
              >>><input type='submit' value='Step 2...'>
              >>></form>
              >>>_HTML_;
              >>>$zipcode = trim($_POST['zipcode']);
              >>>$zip_lengt h = strlen($zipcode );
              >>>print $_POST['zipcode'];
              >>>if ($zip_length != 5){
              >>>print 'invalid amount of characters';
              >>>} else {
              >>>print ' ';
              >>>}
              >>>?>
              >>></code>
              >>>I cannot figure out how to have the the 'invalid amount of characters'
              >>>string post only after the info is passed through the form. Instead it
              >>>displays that string on the bottom of the page without any
              >>>intervention . I know the answer is sitting right in front of my face.
              >>>Maybe someone can help?
              >>>Thanks!
              >>First of all, remember that all of the PHP code on the page runs BEFORE
              >>the page is sent to the client. So your code will run the first time,
              >>giving the output you see.
              >>>
              >>To correct your problem, check to see that the form actually was
              >>submitted, i.e.
              >>>
              >><?php
              >> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
              >> $zipcode = trim($_POST['zipcode']);
              >> $zip_length = strlen($zipcode );
              >> print $_POST['zipcode'];
              >> if ($zip_length != 5){
              >> print 'invalid amount of characters';
              >> } else {
              >> print ' ';
              >>>
              >>}
              >>>
              >>Also, I recommend you move the test earlier in the file, and if you
              >>detect an error, put an error message out right at the field, i.e. just
              >>before the zipcode field. That way the message will be put out just
              >>before error.
              >>>
              >>Alternatively , put the error message(s) at the beginning of the file
              >>(after any header you might have).
              >>>
              >>People look for errors at the error location or the top of the window
              >>not at the bottom of the window.
              >>>
              >>--
              >>============= =====
              >>Remove the "x" from my email address
              >>Jerry Stuckle
              >>JDS Computer Training Corp.
              >>jstuck...@att global.net
              >>============= =====
              >>
              >Thank you so much! Great help.
              >
              Just remember that doing such checks is typically counter productive.
              You put in a zipcode check and later you want to include Canada. Oops!
              >
              This applies to a whole host of checking that attempts to outsmart the
              client.
              >
              If you have to do this you should consider doing this client side,
              before taking them on a trip to the server and back.
              >
              Jeff
              Even if you validate it client-side you need to validate this
              server-side. Never trust what's coming from the client.

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

              Comment

              • Geoff Berrow

                #8
                Re: refactor + help

                Message-ID: <-tedne7jENgB-AnVnZ2dnUVZ_hmd nZ2d@comcast.co mfrom Jerry
                Stuckle contained the following:
                > If you have to do this you should consider doing this client side,
                >before taking them on a trip to the server and back.
                >>
                > Jeff
                >
                >Even if you validate it client-side you need to validate this
                >server-side. Never trust what's coming from the client.
                Yes, client side verification is useful for the customer but is no
                substitute for server side checking.
                --
                Geoff Berrow 011000100110110 0010000000110
                001101101011011 001000110111101 100111001011
                100110001101101 111001011100111 010101101011
                The Slippery Hill Boys Tel: 07985 425932. American themed barn dances and bluegrass performances. Stoke on Trent, Newcastle under Lyme, Staffordshire, Cheshire and surrounding areas.

                Comment

                • C. (http://symcbean.blogspot.com/)

                  #9
                  Re: refactor + help

                  On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                  Jeff wrote:
                  silverDuck wrote:
                  On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                  >silverDuck wrote:
                  >>There is probably an incredibly simple solution for this problem, but
                  >>I can't seem to figure it out. Hopefully one of you gurus can help.
                  >>I'm was messing around with PHP today and I wrote a tiny app that asks
                  >>for your name and zipcode. If your zipcode is less than 5 characters,
                  >>it asks you too enter a zip code with the proper amount of characters.
                  >>Here's the code:
                  >><code>
                  >><?php
                  >>print <<<_HTML_
                  >><form method='POST' action='$_SERVE R[PHP_SELF]'>
                  >>Name: <input type='text' name='name'><br /><br />
                  >>Zipcode: <input type='text' name='zipcode'> <br /><br />
                  >><input type='submit' value='Step 2...'>
                  >></form>
                  >>_HTML_;
                  >>$zipcode = trim($_POST['zipcode']);
                  >>$zip_length = strlen($zipcode );
                  >>print $_POST['zipcode'];
                  >>if ($zip_length != 5){
                  >>print 'invalid amount of characters';
                  >>} else {
                  >>print ' ';
                  >>}
                  >>?>
                  >></code>
                  >>I cannot figure out how to have the the 'invalid amount of characters'
                  >>string post only after the info is passed through the form. Instead it
                  >>displays that string on the bottom of the page without any
                  >>interventio n. I know the answer is sitting right in front of my face.
                  >>Maybe someone can help?
                  >>Thanks!
                  >First of all, remember that all of the PHP code on the page runs BEFORE
                  >the page is sent to the client. So your code will run the first time,
                  >giving the output you see.
                  >
                  >To correct your problem, check to see that the form actually was
                  >submitted, i.e.
                  >
                  ><?php
                  > if (isset($_POST['submit'] && post['submit'] == 'Step 2')
                  > $zipcode = trim($_POST['zipcode']);
                  > $zip_length = strlen($zipcode );
                  > print $_POST['zipcode'];
                  > if ($zip_length != 5){
                  > print 'invalid amount of characters';
                  > } else {
                  > print ' ';
                  >
                  >}
                  >
                  >Also, I recommend you move the test earlier in the file, and if you
                  >detect an error, put an error message out right at the field, i.e. just
                  >before the zipcode field. That way the message will be put out just
                  >before error.
                  >
                  >Alternativel y, put the error message(s) at the beginning of the file
                  >(after any header you might have).
                  >
                  >People look for errors at the error location or the top of the window
                  >not at the bottom of the window.
                  >
                  >--
                  >============== ====
                  >Remove the "x" from my email address
                  >Jerry Stuckle
                  >JDS Computer Training Corp.
                  >jstuck...@attg lobal.net
                  >============== ====
                  >
                  Thank you so much! Great help.
                  >
                  Just remember that doing such checks is typically counter productive.
                  You put in a zipcode check and later you want to include Canada. Oops!
                  >
                  This applies to a whole host of checking that attempts to outsmart the
                  client.
                  >
                  If you have to do this you should consider doing this client side,
                  before taking them on a trip to the server and back.
                  >
                  Jeff
                  >
                  Even if you validate it client-side you need to validate this
                  server-side. Never trust what's coming from the client.
                  >
                  --
                  =============== ===
                  Remove the "x" from my email address
                  Jerry Stuckle
                  JDS Computer Training Corp.
                  jstuck...@attgl obal.net
                  =============== ===
                  IIRC US zipcodes should be 5 digits AND numeric (its advisable to
                  validate as extensively as possible, as early as possible) so I'd
                  suggest using a regular expression rather than just the length of the
                  string. Further, pregs are mostly the same as javascript regexes - so
                  the same regex should work at both ends. Something like /^[0-9]{5,5}$/

                  C.

                  Comment

                  • Jerry Stuckle

                    #10
                    Re: refactor + help

                    C. (http://symcbean.blogspot.com/) wrote:
                    On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    >Jeff wrote:
                    >>silverDuck wrote:
                    >>>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                    >>>>silverDuc k wrote:
                    >>>>>There is probably an incredibly simple solution for this problem, but
                    >>>>>I can't seem to figure it out. Hopefully one of you gurus can help.
                    >>>>>I'm was messing around with PHP today and I wrote a tiny app that asks
                    >>>>>for your name and zipcode. If your zipcode is less than 5 characters,
                    >>>>>it asks you too enter a zip code with the proper amount of characters.
                    >>>>>Here's the code:
                    >>>>><code>
                    >>>>><?php
                    >>>>>print <<<_HTML_
                    >>>>><form method='POST' action='$_SERVE R[PHP_SELF]'>
                    >>>>>Name: <input type='text' name='name'><br /><br />
                    >>>>>Zipcode: <input type='text' name='zipcode'> <br /><br />
                    >>>>><input type='submit' value='Step 2...'>
                    >>>>></form>
                    >>>>>_HTML_;
                    >>>>>$zipcode = trim($_POST['zipcode']);
                    >>>>>$zip_lengt h = strlen($zipcode );
                    >>>>>print $_POST['zipcode'];
                    >>>>>if ($zip_length != 5){
                    >>>>>print 'invalid amount of characters';
                    >>>>>} else {
                    >>>>>print ' ';
                    >>>>>}
                    >>>>>?>
                    >>>>></code>
                    >>>>>I cannot figure out how to have the the 'invalid amount of characters'
                    >>>>>string post only after the info is passed through the form. Instead it
                    >>>>>displays that string on the bottom of the page without any
                    >>>>>interventi on. I know the answer is sitting right in front of my face.
                    >>>>>Maybe someone can help?
                    >>>>>Thanks!
                    >>>>First of all, remember that all of the PHP code on the page runs BEFORE
                    >>>>the page is sent to the client. So your code will run the first time,
                    >>>>giving the output you see.
                    >>>>To correct your problem, check to see that the form actually was
                    >>>>submitted , i.e.
                    >>>><?php
                    >>>> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
                    >>>> $zipcode = trim($_POST['zipcode']);
                    >>>> $zip_length = strlen($zipcode );
                    >>>> print $_POST['zipcode'];
                    >>>> if ($zip_length != 5){
                    >>>> print 'invalid amount of characters';
                    >>>> } else {
                    >>>> print ' ';
                    >>>>}
                    >>>>Also, I recommend you move the test earlier in the file, and if you
                    >>>>detect an error, put an error message out right at the field, i.e. just
                    >>>>before the zipcode field. That way the message will be put out just
                    >>>>before error.
                    >>>>Alternative ly, put the error message(s) at the beginning of the file
                    >>>>(after any header you might have).
                    >>>>People look for errors at the error location or the top of the window
                    >>>>not at the bottom of the window.
                    >>>>--
                    >>>>=========== =======
                    >>>>Remove the "x" from my email address
                    >>>>Jerry Stuckle
                    >>>>JDS Computer Training Corp.
                    >>>>jstuck...@a ttglobal.net
                    >>>>=========== =======
                    >>>Thank you so much! Great help.
                    >> Just remember that doing such checks is typically counter productive.
                    >>You put in a zipcode check and later you want to include Canada. Oops!
                    >> This applies to a whole host of checking that attempts to outsmart the
                    >>client.
                    >> If you have to do this you should consider doing this client side,
                    >>before taking them on a trip to the server and back.
                    >> Jeff
                    >Even if you validate it client-side you need to validate this
                    >server-side. Never trust what's coming from the client.
                    >>
                    >--
                    >============== ====
                    >Remove the "x" from my email address
                    >Jerry Stuckle
                    >JDS Computer Training Corp.
                    >jstuck...@attg lobal.net
                    >============== ====
                    >
                    IIRC US zipcodes should be 5 digits AND numeric (its advisable to
                    validate as extensively as possible, as early as possible) so I'd
                    suggest using a regular expression rather than just the length of the
                    string. Further, pregs are mostly the same as javascript regexes - so
                    the same regex should work at both ends. Something like /^[0-9]{5,5}$/
                    >
                    C.
                    >
                    And what about the 10 character zipcodes, like 12345-6789?

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

                    Comment

                    • C. (http://symcbean.blogspot.com/)

                      #11
                      Re: refactor + help

                      On Aug 4, 4:19 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                      C. (http://symcbean.blogspot.com/) wrote:
                      On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                      Jeff wrote:
                      >silverDuck wrote:
                      >>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                      >>>silverDuck wrote:
                      >>>>There is probably an incredibly simple solution for this problem, but
                      >>>>I can't seem to figure it out. Hopefully one of you gurus can help.
                      >>>>I'm was messing around with PHP today and I wrote a tiny app that asks
                      >>>>for your name and zipcode. If your zipcode is less than 5 characters,
                      >>>>it asks you too enter a zip code with the proper amount of characters.
                      >>>>Here's the code:
                      >>>><code>
                      >>>><?php
                      >>>>print <<<_HTML_
                      >>>><form method='POST' action='$_SERVE R[PHP_SELF]'>
                      >>>>Name: <input type='text' name='name'><br /><br />
                      >>>>Zipcode: <input type='text' name='zipcode'> <br /><br />
                      >>>><input type='submit' value='Step 2...'>
                      >>>></form>
                      >>>>_HTML_;
                      >>>>$zipcode = trim($_POST['zipcode']);
                      >>>>$zip_leng th = strlen($zipcode );
                      >>>>print $_POST['zipcode'];
                      >>>>if ($zip_length != 5){
                      >>>>print 'invalid amount of characters';
                      >>>>} else {
                      >>>>print ' ';
                      >>>>}
                      >>>>?>
                      >>>></code>
                      >>>>I cannot figure out how to have the the 'invalid amount of characters'
                      >>>>string post only after the info is passed through the form. Instead it
                      >>>>displays that string on the bottom of the page without any
                      >>>>interventio n. I know the answer is sitting right in front of my face.
                      >>>>Maybe someone can help?
                      >>>>Thanks!
                      >>>First of all, remember that all of the PHP code on the page runs BEFORE
                      >>>the page is sent to the client. So your code will run the first time,
                      >>>giving the output you see.
                      >>>To correct your problem, check to see that the form actually was
                      >>>submitted, i.e.
                      >>><?php
                      >>> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
                      >>> $zipcode = trim($_POST['zipcode']);
                      >>> $zip_length = strlen($zipcode );
                      >>> print $_POST['zipcode'];
                      >>> if ($zip_length != 5){
                      >>> print 'invalid amount of characters';
                      >>> } else {
                      >>> print ' ';
                      >>>}
                      >>>Also, I recommend you move the test earlier in the file, and if you
                      >>>detect an error, put an error message out right at the field, i.e. just
                      >>>before the zipcode field. That way the message will be put out just
                      >>>before error.
                      >>>Alternativel y, put the error message(s) at the beginning of the file
                      >>>(after any header you might have).
                      >>>People look for errors at the error location or the top of the window
                      >>>not at the bottom of the window.
                      >>>--
                      >>>============ ======
                      >>>Remove the "x" from my email address
                      >>>Jerry Stuckle
                      >>>JDS Computer Training Corp.
                      >>>jstuck...@at tglobal.net
                      >>>============ ======
                      >>Thank you so much! Great help.
                      > Just remember that doing such checks is typically counter productive.
                      >You put in a zipcode check and later you want to include Canada. Oops!
                      > This applies to a whole host of checking that attempts to outsmart the
                      >client.
                      > If you have to do this you should consider doing this client side,
                      >before taking them on a trip to the server and back.
                      > Jeff
                      Even if you validate it client-side you need to validate this
                      server-side. Never trust what's coming from the client.
                      >
                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstuck...@attgl obal.net
                      =============== ===
                      >
                      IIRC US zipcodes should be 5 digits AND numeric (its advisable to
                      validate as extensively as possible, as early as possible) so I'd
                      suggest using a regular expression rather than just the length of the
                      string. Further, pregs are mostly the same as javascript regexes - so
                      the same regex should work at both ends. Something like /^[0-9]{5,5}$/
                      >
                      C.
                      >
                      And what about the 10 character zipcodes, like 12345-6789?
                      >
                      --
                      =============== ===
                      Remove the "x" from my email address
                      Jerry Stuckle
                      JDS Computer Training Corp.
                      jstuck...@attgl obal.net
                      =============== ===
                      You can't work out what the regex for that should be?

                      C.

                      Comment

                      • Jerry Stuckle

                        #12
                        Re: refactor + help

                        C. (http://symcbean.blogspot.com/) wrote:
                        On Aug 4, 4:19 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                        >C. (http://symcbean.blogspot.com/) wrote:
                        >>On Aug 2, 2:43 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                        >>>Jeff wrote:
                        >>>>silverDuc k wrote:
                        >>>>>On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                        >>>>>>silverDuc k wrote:
                        >>>>>>>There is probably an incredibly simple solution for this problem, but
                        >>>>>>>I can't seem to figure it out. Hopefully one of you gurus can help.
                        >>>>>>>I'm was messing around with PHP today and I wrote a tiny app that asks
                        >>>>>>>for your name and zipcode. If your zipcode is less than 5 characters,
                        >>>>>>>it asks you too enter a zip code with the proper amount of characters.
                        >>>>>>>Here's the code:
                        >>>>>>><code>
                        >>>>>>><?php
                        >>>>>>>print <<<_HTML_
                        >>>>>>><form method='POST' action='$_SERVE R[PHP_SELF]'>
                        >>>>>>>Name: <input type='text' name='name'><br /><br />
                        >>>>>>>Zipcod e: <input type='text' name='zipcode'> <br /><br />
                        >>>>>>><input type='submit' value='Step 2...'>
                        >>>>>>></form>
                        >>>>>>>_HTML_ ;
                        >>>>>>>$zipco de = trim($_POST['zipcode']);
                        >>>>>>>$zip_len gth = strlen($zipcode );
                        >>>>>>>print $_POST['zipcode'];
                        >>>>>>>if ($zip_length != 5){
                        >>>>>>>print 'invalid amount of characters';
                        >>>>>>>} else {
                        >>>>>>>print ' ';
                        >>>>>>>}
                        >>>>>>>?>
                        >>>>>>></code>
                        >>>>>>>I cannot figure out how to have the the 'invalid amount of characters'
                        >>>>>>>string post only after the info is passed through the form. Instead it
                        >>>>>>>displa ys that string on the bottom of the page without any
                        >>>>>>>interven tion. I know the answer is sitting right in front of my face.
                        >>>>>>>Maybe someone can help?
                        >>>>>>>Thanks !
                        >>>>>>First of all, remember that all of the PHP code on the page runs BEFORE
                        >>>>>>the page is sent to the client. So your code will run the first time,
                        >>>>>>giving the output you see.
                        >>>>>>To correct your problem, check to see that the form actually was
                        >>>>>>submitted , i.e.
                        >>>>>><?php
                        >>>>>> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
                        >>>>>> $zipcode = trim($_POST['zipcode']);
                        >>>>>> $zip_length = strlen($zipcode );
                        >>>>>> print $_POST['zipcode'];
                        >>>>>> if ($zip_length != 5){
                        >>>>>> print 'invalid amount of characters';
                        >>>>>> } else {
                        >>>>>> print ' ';
                        >>>>>>}
                        >>>>>>Also, I recommend you move the test earlier in the file, and if you
                        >>>>>>detect an error, put an error message out right at the field, i.e. just
                        >>>>>>before the zipcode field. That way the message will be put out just
                        >>>>>>before error.
                        >>>>>>Alternati vely, put the error message(s) at the beginning of the file
                        >>>>>>(after any header you might have).
                        >>>>>>People look for errors at the error location or the top of the window
                        >>>>>>not at the bottom of the window.
                        >>>>>>--
                        >>>>>>========= =========
                        >>>>>>Remove the "x" from my email address
                        >>>>>>Jerry Stuckle
                        >>>>>>JDS Computer Training Corp.
                        >>>>>>jstuck... @attglobal.net
                        >>>>>>========= =========
                        >>>>>Thank you so much! Great help.
                        >>>> Just remember that doing such checks is typically counter productive.
                        >>>>You put in a zipcode check and later you want to include Canada. Oops!
                        >>>> This applies to a whole host of checking that attempts to outsmart the
                        >>>>client.
                        >>>> If you have to do this you should consider doing this client side,
                        >>>>before taking them on a trip to the server and back.
                        >>>> Jeff
                        >>>Even if you validate it client-side you need to validate this
                        >>>server-side. Never trust what's coming from the client.
                        >>>--
                        >>>============ ======
                        >>>Remove the "x" from my email address
                        >>>Jerry Stuckle
                        >>>JDS Computer Training Corp.
                        >>>jstuck...@at tglobal.net
                        >>>============ ======
                        >>IIRC US zipcodes should be 5 digits AND numeric (its advisable to
                        >>validate as extensively as possible, as early as possible) so I'd
                        >>suggest using a regular expression rather than just the length of the
                        >>string. Further, pregs are mostly the same as javascript regexes - so
                        >>the same regex should work at both ends. Something like /^[0-9]{5,5}$/
                        >>C.
                        >And what about the 10 character zipcodes, like 12345-6789?
                        >>
                        >--
                        >============== ====
                        >Remove the "x" from my email address
                        >Jerry Stuckle
                        >JDS Computer Training Corp.
                        >jstuck...@attg lobal.net
                        >============== ====
                        >
                        You can't work out what the regex for that should be?
                        >
                        C.
                        >
                        Sure I can. But my point is zip codes in the U.S. are not necessarily 5
                        digits.

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

                        Comment

                        • edmund yau

                          #13
                          Re: refactor + help

                          Jeff 提到:
                          silverDuck wrote:
                          >On Aug 1, 8:33 pm, Jerry Stuckle <jstuck...@attg lobal.netwrote:
                          >>silverDuck wrote:
                          >>>There is probably an incredibly simple solution for this problem, but
                          >>>I can't seem to figure it out. Hopefully one of you gurus can help.
                          >>>I'm was messing around with PHP today and I wrote a tiny app that asks
                          >>>for your name and zipcode. If your zipcode is less than 5 characters,
                          >>>it asks you too enter a zip code with the proper amount of characters.
                          >>>Here's the code:
                          >>><code>
                          >>><?php
                          >>>print <<<_HTML_
                          >>><form method='POST' action='$_SERVE R[PHP_SELF]'>
                          >>>Name: <input type='text' name='name'><br /><br />
                          >>>Zipcode: <input type='text' name='zipcode'> <br /><br />
                          >>><input type='submit' value='Step 2...'>
                          >>></form>
                          >>>_HTML_;
                          >>>$zipcode = trim($_POST['zipcode']);
                          >>>$zip_lengt h = strlen($zipcode );
                          >>>print $_POST['zipcode'];
                          >>>if ($zip_length != 5){
                          >>>print 'invalid amount of characters';
                          >>>} else {
                          >>>print ' ';
                          >>>}
                          >>>?>
                          >>></code>
                          >>>I cannot figure out how to have the the 'invalid amount of characters'
                          >>>string post only after the info is passed through the form. Instead it
                          >>>displays that string on the bottom of the page without any
                          >>>intervention . I know the answer is sitting right in front of my face.
                          >>>Maybe someone can help?
                          >>>Thanks!
                          >>First of all, remember that all of the PHP code on the page runs BEFORE
                          >>the page is sent to the client. So your code will run the first time,
                          >>giving the output you see.
                          >>>
                          >>To correct your problem, check to see that the form actually was
                          >>submitted, i.e.
                          >>>
                          >><?php
                          >> if (isset($_POST['submit'] && post['submit'] == 'Step 2')
                          >> $zipcode = trim($_POST['zipcode']);
                          >> $zip_length = strlen($zipcode );
                          >> print $_POST['zipcode'];
                          >> if ($zip_length != 5){
                          >> print 'invalid amount of characters';
                          >> } else {
                          >> print ' ';
                          >>>
                          >>}
                          >>>
                          >>Also, I recommend you move the test earlier in the file, and if you
                          >>detect an error, put an error message out right at the field, i.e. just
                          >>before the zipcode field. That way the message will be put out just
                          >>before error.
                          >>>
                          >>Alternatively , put the error message(s) at the beginning of the file
                          >>(after any header you might have).
                          >>>
                          >>People look for errors at the error location or the top of the window
                          >>not at the bottom of the window.
                          >>>
                          >>--
                          >>============= =====
                          >>Remove the "x" from my email address
                          >>Jerry Stuckle
                          >>JDS Computer Training Corp.
                          >>jstuck...@att global.net
                          >>============= =====
                          >>
                          >Thank you so much! Great help.
                          >
                          Just remember that doing such checks is typically counter productive.
                          You put in a zipcode check and later you want to include Canada. Oops!
                          >
                          This applies to a whole host of checking that attempts to outsmart the
                          client.
                          >
                          If you have to do this you should consider doing this client side,
                          before taking them on a trip to the server and back.
                          >
                          Jeff
                          IT manager 蛇頭鼠眼edmund yau (email擬似yau tinho@hotmail.c om)
                          全香港最濺 IT Manager, 見你著西裝返工 . 著得好過佢 就問你夠竟點諗, 正小人,
                          ç—´æ’šç·š
                          5:00pm俾個job 你做, 跟住就自己 做到11é»žæžæ Ž‚ä½¢, 第2日就小你, 話你搞唔掂, å””
                          撚洗放工呀

                          最撚柒就係 呢條友啦, 搞個爛網出 黎, 大家去睇下 :
                          網址擬似www .funzy.com

                          有幾爛? 仆街, 用IE6開會死 機架, 真係唔撚知 用乜野skillå ¯ä»¥å¯«æ’šåˆ°h ang機.

                          成條team朝9æ ™š10, 搞個爛網得 個2個function ä»”, 唔係hangæ©Ÿå° ±ä¿‚æ­»link, 仲話寫
                          web要都唔知 乜撚野skill set, 食屎啦, 小學生寫個 web都唔撚會 hang機啦

                          個個同事想 放工都要驚 呢樣驚個樣 , 你張你oçš„é’æ˜¥æ¿ºè³ £å°±å””æ’šå¥½å «äººä¸€é½ŠåŒä½ æ¿º
                          賣, 人地都有屋 企, 你唔撚放工就 你o既事, 除撚左打工 都唔撚知搞 個web未, 屎坑關刀

                          大家多多張 呢個網發佈 出去, 聲討呢種小 人



                          Counter
                          1

                          Comment

                          Working...