Form Validation

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

    #1

    Form Validation

    I understand how to validate form data when a form is submitted to
    'self' (ie. action="$_SERVE R_[PHP_SELF]"). In this case you simply
    validate before processing. However, what if submitting the form to
    another page (ie. action="someoth erpage.php"). You could validate the
    form data on the new page but it makes more sense to validate first
    before submitting, plus if there are errors, then you have to send the
    user back to the first page. There must be a way to do the error
    checking before the submit. The only way I can think to do this is with
    Javascript and a button element onclick event (instead of Submit button)
    with the Javascript handling the form validation and form submit. There
    is probably a real obvious PHP solution but I can't see it.
  • Daniel Tryba

    #2
    Re: Form Validation

    peebrain <pb@anon.com> wrote:[color=blue]
    > There must be a way to do the error checking before the submit. The
    > only way I can think to do this is with Javascript and a button
    > element onclick event (instead of Submit button) with the Javascript
    > handling the form validation and form submit. There is probably a real
    > obvious PHP solution but I can't see it.[/color]

    It's very obvious that there is _NO_ way to validate with PHP before
    submitting it since PHP is serverside and to get the data from
    clientside to serverside it has to be submitted (one way or the other).

    Validation needs to be done serverside always, any checks clientside are
    a bonus but are not to be relied on.

    Comment

    • peebrain

      #3
      Re: Form Validation

      Daniel Tryba wrote:[color=blue]
      > peebrain <pb@anon.com> wrote:
      >[color=green]
      >>There must be a way to do the error checking before the submit. The[/color]
      >
      >
      > It's very obvious that there is _NO_ way to validate with PHP before
      > submitting it since PHP is serverside and to get the data from
      > clientside to serverside it has to be submitted (one way or the other).
      >
      > Validation needs to be done serverside always, any checks clientside are
      > a bonus but are not to be relied on.
      >[/color]

      Thanx for the prompt response Daniel. Ok, that makes sense of course. So
      clearly I can do some client-side validation with JavaScript but should
      also being doing it on the server-side. My problem though is if form
      validation fails on "page 2.php" (where the form data was submitted and
      the validation performed), how do I return the user to "page1.php" to
      try again?

      Comment

      • Ken Robinson

        #4
        Re: Form Validation



        peebrain wrote:
        [color=blue]
        >
        > Thanx for the prompt response Daniel. Ok, that makes sense of course. So
        > clearly I can do some client-side validation with JavaScript but should
        > also being doing it on the server-side. My problem though is if form
        > validation fails on "page 2.php" (where the form data was submitted and
        > the validation performed), how do I return the user to "page1.php" to
        > try again?[/color]

        Why do you need separate scripts?

        To answer your question, you use the header() function.


        Ken

        Comment

        • Geoff Berrow

          #5
          Re: Form Validation

          I noticed that Message-ID: <bJ0ve.98809$on 1.49016@clgrps1 3> from
          peebrain contained the following:
          [color=blue]
          >My problem though is if form
          >validation fails on "page 2.php" (where the form data was submitted and
          >the validation performed), how do I return the user to "page1.php" to
          >try again?[/color]

          You could always tell them to press the back button.

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

          • Gary L. Burnore

            #6
            Re: Form Validation

            On Sat, 25 Jun 2005 00:43:29 +0100, Geoff Berrow
            <blthecat@ckdog .co.uk> wrote:
            [color=blue]
            >I noticed that Message-ID: <bJ0ve.98809$on 1.49016@clgrps1 3> from
            >peebrain contained the following:
            >[color=green]
            >>My problem though is if form
            >>validation fails on "page 2.php" (where the form data was submitted and
            >>the validation performed), how do I return the user to "page1.php" to
            >>try again?[/color]
            >
            >You could always tell them to press the back button.[/color]

            Or:

            if (whatever determines failure) {

            echo "<META HTTP-EQUIV=\"refresh \" CONTENT=\"0 ;URL=\"page1.ph p\">";
            echo $Refresh;

            }


            --
            gburnore@databa six dot com
            ---------------------------------------------------------------------------
            How you look depends on where you go.
            ---------------------------------------------------------------------------
            Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
            Black Helicopter Repair Svcs Division | Official Proof of Purchase
            =============== =============== =============== =============== ===============
            Want one? GET one! http://signup.databasix.com
            =============== =============== =============== =============== ===============

            Comment

            • Daniel Tryba

              #7
              Re: Form Validation

              Geoff Berrow <blthecat@ckdog .co.uk> wrote:[color=blue][color=green]
              >>My problem though is if form
              >>validation fails on "page 2.php" (where the form data was submitted and
              >>the validation performed), how do I return the user to "page1.php" to
              >>try again?[/color]
              >
              > You could always tell them to press the back button.[/color]

              How very userfriendly. Server decides that something is wrong and that
              the user should fix it by themselves by backing up a few pages. Some
              problems it cause:
              -what was wrong again?
              -password fields tend to be empty in some browsers (good thing (tm))
              -other fields may be reset to defaults (eg. select)

              Comment

              • peebrain

                #8
                Re: Form Validation

                Ken Robinson wrote:[color=blue]
                >
                > peebrain wrote:
                >
                >[color=green]
                >>... My problem though is if form
                >>validation fails on "page 2.php" (where the form data was submitted and
                >>the validation performed), how do I return the user to "page1.php" to
                >>try again?[/color]
                >
                >
                > Why do you need separate scripts?
                >
                > To answer your question, you use the header() function.
                > www.php.net/header
                >
                > Ken
                >[/color]

                Good one Ken. That will be very useful if not in this case then in other
                situations where I want to redirect to another page.
                8< --------------
                header("Locatio n: http://www.example.com/"); /* Redirect browser */
                8< --------------
                thanx.
                The reason for two pages is that the data submitted by the first form
                gets loaded up into a different form. This could all be done in one
                script but logically made sense to two separate purpose scripts.

                Comment

                • peebrain

                  #9
                  Re: Form Validation

                  Gary L. Burnore wrote:[color=blue]
                  > On Sat, 25 Jun 2005 00:43:29 +0100, Geoff Berrow
                  > <blthecat@ckdog .co.uk> wrote:
                  >
                  >[color=green]
                  >>I noticed that Message-ID: <bJ0ve.98809$on 1.49016@clgrps1 3> from
                  >>peebrain contained the following:
                  >>
                  >>[color=darkred]
                  >>>My problem though is if form
                  >>>validation fails on "page 2.php" (where the form data was submitted and
                  >>>the validation performed), how do I return the user to "page1.php" to
                  >>>try again?[/color]
                  >>
                  >>You could always tell them to press the back button.[/color]
                  >
                  >
                  > Or:
                  >
                  > if (whatever determines failure) {
                  >
                  > echo "<META HTTP-EQUIV=\"refresh \" CONTENT=\"0 ;URL=\"page1.ph p\">";
                  > echo $Refresh;
                  >
                  > }
                  >
                  >[/color]

                  This is also a good one. Thanx Gary. This or the header() function
                  mentioned by Ken will both accomplish the redirect.

                  Comment

                  • Manuel Lemos

                    #10
                    Re: Form Validation

                    Hello,

                    on 06/24/2005 07:51 PM peebrain said the following:[color=blue]
                    > I understand how to validate form data when a form is submitted to
                    > 'self' (ie. action="$_SERVE R_[PHP_SELF]"). In this case you simply
                    > validate before processing. However, what if submitting the form to
                    > another page (ie. action="someoth erpage.php"). You could validate the
                    > form data on the new page but it makes more sense to validate first
                    > before submitting, plus if there are errors, then you have to send the
                    > user back to the first page. There must be a way to do the error
                    > checking before the submit. The only way I can think to do this is with
                    > Javascript and a button element onclick event (instead of Submit button)
                    > with the Javascript handling the form validation and form submit. There
                    > is probably a real obvious PHP solution but I can't see it.[/color]

                    You may want to try this class that performs both client side
                    (Javascript) and server side (PHP) validation from the same set of rules.

                    http://www.phpclasses. org/formsgeneration


                    --

                    Regards,
                    Manuel Lemos

                    PHP Classes - Free ready to use OOP components written in PHP
                    http://www.phpclasses. org/

                    PHP Reviews - Reviews of PHP books and other products
                    http://www.phpclasses. org/reviews/

                    Metastorage - Data object relational mapping layer generator
                    http://www.meta-language.net/metastorage.htm l

                    Comment

                    • Peter Fox

                      #11
                      Re: Form Validation

                      Following on from Daniel Tryba's message. . .[color=blue]
                      >Geoff Berrow <blthecat@ckdog .co.uk> wrote:[color=green][color=darkred]
                      >>>My problem though is if form
                      >>>validation fails on "page 2.php" (where the form data was submitted and
                      >>>the validation performed), how do I return the user to "page1.php" to
                      >>>try again?[/color]
                      >>
                      >> You could always tell them to press the back button.[/color]
                      >
                      >How very userfriendly. Server decides that something is wrong and that
                      >the user should fix it by themselves by backing up a few pages. Some
                      >problems it cause:
                      >-what was wrong again?
                      >-password fields tend to be empty in some browsers (good thing (tm))
                      >-other fields may be reset to defaults (eg. select)
                      >[/color]
                      Quite agree 90% of time, as if there is _no decision_ to be made by the
                      user except to type in the damn thing correctly then the suitable page
                      should be displayed automatically there and then with _informative,
                      unambiguous and well-positioned error message_.

                      However there are situations where the 'error' is not a matter of sloppy
                      input but logic known to the system only. For example : "You have
                      ordered two sweets but no main course do you really want to do this?".
                      In this case 'back' is a logical option in the minds of some users. I
                      would _not_ expect or encourage users to use Back (or forward) buttons
                      and therefore provide proper yes and no buttons on the screen to do this
                      *but* some users will use the Back button regardless and so your screen
                      logic needs to be able to cope with this 'mis-use'.




                      --
                      PETER FOX Not the same since the bottom fell out of the bucket business
                      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
                      2 Tees Close, Witham, Essex.
                      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

                      Comment

                      • Geoff Berrow

                        #12
                        Re: Form Validation

                        I noticed that Message-ID: <42bca25c$0$377 51$c5fe704e@new s6.xs4all.nl>
                        from Daniel Tryba contained the following:
                        [color=blue][color=green]
                        >> You could always tell them to press the back button.[/color]
                        >
                        >How very userfriendly. Server decides that something is wrong and that
                        >the user should fix it by themselves by backing up a few pages.[/color]

                        Please note I said /could/. I'll admit it's a quick and dirty solution
                        (though I use it in my mail script where the originating page is bog
                        standard html with no php).

                        Using some kind of redirect will involve passing the input back to the
                        form. Which means page 1 has to have the capacity to redisplay them.
                        And if you are going to do that you may as well build in the validation
                        into page 1 and simply redirect to a thank you page on success.
                        --
                        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...