Form validation

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

    Form validation

    I am looking into form validation for my HTML form. I don't want to use
    javascript and was looking into using PHP. A crazy thought crossed my
    mind and I just wanted to get some input.

    What if I basically made the form one long if/then statement? I could
    ask for the first piece of information, test it against some
    conditions, then if it is okay I allow the user to fill in the next
    piece of information. I could go do down the page validation each box
    as we go. I could use hidden on all the text boxes until the previous
    condition had been validated. Some pseudocode:

    <?php
    <form action=POST method=register ed.php>
    <input name"Fname" type="text">
    if ($Fname meets some condition)
    {
    <input name="Lname" type="text">
    if ($Lname meets some condition)
    {
    <input name="Number" type="text>
    if ($Number meets some condition)
    {
    }
    }
    }
    else;
    echo "Please enter a phone number";
    else;
    echo "Please enter a valid last name";
    else;
    echo "Please enter a valid first name";
    ?>

    I know the code isn't exact, I am just theorizing here. Could such a
    thing be done? Even remotely similar? I don't really plan on doing this
    anytime soon, but I was just curious. I only started PHP programming a
    few days ago so no need to point out how stupid this may sound.

  • Jerry Stuckle

    #2
    Re: Form validation

    Jerim79 wrote:
    I am looking into form validation for my HTML form. I don't want to use
    javascript and was looking into using PHP. A crazy thought crossed my
    mind and I just wanted to get some input.
    >
    What if I basically made the form one long if/then statement? I could
    ask for the first piece of information, test it against some
    conditions, then if it is okay I allow the user to fill in the next
    piece of information. I could go do down the page validation each box
    as we go. I could use hidden on all the text boxes until the previous
    condition had been validated. Some pseudocode:
    >
    <?php
    <form action=POST method=register ed.php>
    <input name"Fname" type="text">
    if ($Fname meets some condition)
    {
    <input name="Lname" type="text">
    if ($Lname meets some condition)
    {
    <input name="Number" type="text>
    if ($Number meets some condition)
    {
    }
    }
    }
    else;
    echo "Please enter a phone number";
    else;
    echo "Please enter a valid last name";
    else;
    echo "Please enter a valid first name";
    ?>
    >
    I know the code isn't exact, I am just theorizing here. Could such a
    thing be done? Even remotely similar? I don't really plan on doing this
    anytime soon, but I was just curious. I only started PHP programming a
    few days ago so no need to point out how stupid this may sound.
    >
    Jerim,

    Yes and no.

    First of all, the PHP code will be executed before the page is ever sent
    to the client. It's not interactive like javascript is.

    So what you do is have the user complete the form and submit it. Your
    code should then validate all fields. But you don't want to nest your
    statements like that - for instance, what happens if they don't enter
    either a first name or a phone number? They'll get a single message
    which they correct - and they'll get a second message.

    Rather, process each field individually, i.e.


    $emsg = '';
    if ($fname = '') // No first name
    $emsg .= "Please enter a first name.<br>\n";
    if ($lname = '') // No last name
    $emsg .= "Please enter a last name.<br>\n";
    if ($phone = '') // No phone number
    $emsg .= "Please enter a phone number<br>\n";

    ....

    if ($emsg != '')
    echo $emsg;


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

    Comment

    • Gordon Burditt

      #3
      Re: Form validation

      >I am looking into form validation for my HTML form. I don't want to use
      >javascript and was looking into using PHP. A crazy thought crossed my
      >mind and I just wanted to get some input.
      >What if I basically made the form one long if/then statement? I could
      HTML does not have if/then statements.

      PHP runs server-side. It doesn't run until the user fills out the
      whole form. If you want to ask for each individual item one at
      a time, that's OK, but pretty darned annoying, and a lot of users
      are going to give up.

      JavaScript is often Turned Off (tm).
      >ask for the first piece of information, test it against some
      >conditions, then if it is okay I allow the user to fill in the next
      >piece of information. I could go do down the page validation each box
      >as we go. I could use hidden on all the text boxes until the previous
      >condition had been validated.
      Why? To annoy users and make them go away?

      Note that you *MUST* validate any security-related restrictions on
      the server side *ANYWAY* because spambots and thieves will probably
      have Javascript turned off and bypass validation to steal stuff or
      send SPAM through your server.
      >Some pseudocode:
      >
      ><?php
      ><form action=POST method=register ed.php>
      ><input name"Fname" type="text">
      >if ($Fname meets some condition)
      {
      <input name="Lname" type="text">
      if ($Lname meets some condition)
      {
      <input name="Number" type="text>
      if ($Number meets some condition)
      {
      }
      }
      >}
      >else;
      echo "Please enter a phone number";
      >else;
      echo "Please enter a valid last name";
      >else;
      echo "Please enter a valid first name";
      >?>
      >
      >I know the code isn't exact, I am just theorizing here. Could such a
      >thing be done? Even remotely similar? I don't really plan on doing this
      >anytime soon, but I was just curious. I only started PHP programming a
      >few days ago so no need to point out how stupid this may sound.
      >

      Comment

      • Jerim79

        #4
        Re: Form validation


        Jerry Stuckle wrote:
        Jerim79 wrote:
        I am looking into form validation for my HTML form. I don't want to use
        javascript and was looking into using PHP. A crazy thought crossed my
        mind and I just wanted to get some input.

        What if I basically made the form one long if/then statement? I could
        ask for the first piece of information, test it against some
        conditions, then if it is okay I allow the user to fill in the next
        piece of information. I could go do down the page validation each box
        as we go. I could use hidden on all the text boxes until the previous
        condition had been validated. Some pseudocode:

        <?php
        <form action=POST method=register ed.php>
        <input name"Fname" type="text">
        if ($Fname meets some condition)
        {
        <input name="Lname" type="text">
        if ($Lname meets some condition)
        {
        <input name="Number" type="text>
        if ($Number meets some condition)
        {
        }
        }
        }
        else;
        echo "Please enter a phone number";
        else;
        echo "Please enter a valid last name";
        else;
        echo "Please enter a valid first name";
        ?>

        I know the code isn't exact, I am just theorizing here. Could such a
        thing be done? Even remotely similar? I don't really plan on doing this
        anytime soon, but I was just curious. I only started PHP programming a
        few days ago so no need to point out how stupid this may sound.
        >
        Jerim,
        >
        Yes and no.
        >
        First of all, the PHP code will be executed before the page is ever sent
        to the client. It's not interactive like javascript is.
        >
        So what you do is have the user complete the form and submit it. Your
        code should then validate all fields. But you don't want to nest your
        statements like that - for instance, what happens if they don't enter
        either a first name or a phone number? They'll get a single message
        which they correct - and they'll get a second message.
        >
        Rather, process each field individually, i.e.
        >
        >
        $emsg = '';
        if ($fname = '') // No first name
        $emsg .= "Please enter a first name.<br>\n";
        if ($lname = '') // No last name
        $emsg .= "Please enter a last name.<br>\n";
        if ($phone = '') // No phone number
        $emsg .= "Please enter a phone number<br>\n";
        >
        ...
        >
        if ($emsg != '')
        echo $emsg;
        >
        >
        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===
        Thanks for the info, it was very useful and informative.

        Comment

        • Jerim79

          #5
          Re: Form validation


          Jerim79 wrote:
          I am looking into form validation for my HTML form. I don't want to use
          javascript and was looking into using PHP. A crazy thought crossed my
          mind and I just wanted to get some input.
          >
          What if I basically made the form one long if/then statement? I could
          ask for the first piece of information, test it against some
          conditions, then if it is okay I allow the user to fill in the next
          piece of information. I could go do down the page validation each box
          as we go. I could use hidden on all the text boxes until the previous
          condition had been validated. Some pseudocode:
          >
          <?php
          <form action=POST method=register ed.php>
          <input name"Fname" type="text">
          if ($Fname meets some condition)
          {
          <input name="Lname" type="text">
          if ($Lname meets some condition)
          {
          <input name="Number" type="text>
          if ($Number meets some condition)
          {
          }
          }
          }
          else;
          echo "Please enter a phone number";
          else;
          echo "Please enter a valid last name";
          else;
          echo "Please enter a valid first name";
          ?>
          >
          I know the code isn't exact, I am just theorizing here. Could such a
          thing be done? Even remotely similar? I don't really plan on doing this
          anytime soon, but I was just curious. I only started PHP programming a
          few days ago so no need to point out how stupid this may sound.
          I am just starting work on editing a PHP script that someone else in
          the company wrote. I noticed that this person chose to use different
          PHP pages depending on the error. For instance, the main form contains
          input boxes for Name, Address, Telephone and Email. When submitted, the
          form calls a file called verify_email.ph p. This file runs an if/then
          statement. If the email is valid it calls another form, such as
          verify_name and so on until everything is validated and the information
          is written to the database. If the email is invalid, it displays a new
          form which is similar to the original form, only this one has the email
          box highlighted in red and asks the user to enter the email again. All
          the input boxes are already filled in as the coder chose to use PHP
          variables to re-enter the information for the customer, instead of
          having to start over.

          The question I have is, is this an "acceptable " way to handle error
          messages. Being quite new to PHP I don't know what to make of this
          method.

          Comment

          • Geoff Berrow

            #6
            Re: Form validation

            Message-ID: <1163196797.736 817.324440@m7g2 000cwm.googlegr oups.comfrom
            Jerim79 contained the following:
            >The question I have is, is this an "acceptable " way to handle error
            >messages. Being quite new to PHP I don't know what to make of this
            >method.

            That's very subjective. It may not be terribly elegant but it also may
            be that this solution makes it clear to see what is going on.

            In judging 'acceptability' factors to consider are:

            Is the code secure, independent of user configuration?
            Does the code use significantly more resources than it should?
            Is the code easy to follow, maintain and update?
            Is the solution good and clear from the user's point of view?



            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • Jerim79

              #7
              Re: Form validation


              Geoff Berrow wrote:
              Message-ID: <1163196797.736 817.324440@m7g2 000cwm.googlegr oups.comfrom
              Jerim79 contained the following:
              >
              The question I have is, is this an "acceptable " way to handle error
              messages. Being quite new to PHP I don't know what to make of this
              method.
              >
              >
              That's very subjective. It may not be terribly elegant but it also may
              be that this solution makes it clear to see what is going on.
              >
              In judging 'acceptability' factors to consider are:
              >
              Is the code secure, independent of user configuration?
              Does the code use significantly more resources than it should?
              Is the code easy to follow, maintain and update?
              Is the solution good and clear from the user's point of view?
              >
              >
              >
              --
              Geoff Berrow (put thecat out to email)
              It's only Usenet, no one dies.
              My opinions, not the committee's, mine.
              Simple RFDs http://www.ckdog.co.uk/rfdmaker/
              How can you determine the resources that the code will use? Is there a
              measurable way to determine which code is more "efficient" in terms of
              resources?

              You ask if the code is easy to follow, maintain and update? I would say
              that yes, it is, since even a beginner like myself figured out what the
              code was doing. However, certainly in any language there may be faster,
              harder to understand shortcuts to any solution that a beginner may not
              be aware of. Should we always strive to use a simpler method that
              everyone can figure out, or should we look to use the most advanced
              techniques that may be more efficient but alienate younger, less
              experienced programmers?

              Comment

              • Geoff Berrow

                #8
                Re: Form validation

                Message-ID: <1163430484.409 239.94080@h54g2 000cwb.googlegr oups.comfrom
                Jerim79 contained the following:
                >How can you determine the resources that the code will use? Is there a
                >measurable way to determine which code is more "efficient" in terms of
                >resources?
                Well I'd say it would be some intensive process such as image
                manipulation. If the script constantly generated, say, thumbnails on
                the fly instead of generating them once then subsequently using the
                pre-generated image, then that would obviously use considerably more
                resources. Speed differences are often discussed here, but often the
                differences are small fractions of a second.
                >
                >You ask if the code is easy to follow, maintain and update? I would say
                >that yes, it is, since even a beginner like myself figured out what the
                >code was doing. However, certainly in any language there may be faster,
                >harder to understand shortcuts to any solution that a beginner may not
                >be aware of. Should we always strive to use a simpler method that
                >everyone can figure out, or should we look to use the most advanced
                >techniques that may be more efficient but alienate younger, less
                >experienced programmers?
                I'd say it was personal preference. I happen to think a bit of extra
                verbosity is no bad thing if it improves clarity but obviously, mileage
                varies.
                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                Working...