prevent blank data

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

    prevent blank data

    How do I prevent blank data from being entered into a table?

    For instance, the user fills out a form, but if a field is left blank, then
    the entire entry won't be filled in. This isn't necessarily a form
    validation thing, just that if all the fields are filled, then proceed to
    enter data, if not then don't.

    Thanks in advance!
  • Pedro Graca

    #2
    Re: prevent blank data

    John wrote:[color=blue]
    > How do I prevent blank data from being entered into a table?
    >
    > For instance, the user fills out a form, but if a field is left blank, then
    > the entire entry won't be filled in. This isn't necessarily a form
    > validation thing, just that if all the fields are filled, then proceed to
    > enter data, if not then don't.[/color]

    <?php
    $empty_fields = 0;
    foreach ($_POST as $field) {
    if (empty($field)) ++$empty_fields ;
    }
    if ($empty_fields == 0) {
    // save to database
    }
    ?>


    HTH
    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • John

      #3
      Re: prevent blank data

      Pedro Graca <hexkid@hotpop. com> wrote in
      news:c4fc12$2gq p5j$2@ID-203069.news.uni-berlin.de:[color=blue]
      >
      > <?php
      > $empty_fields = 0;
      > foreach ($_POST as $field) {
      > if (empty($field)) ++$empty_fields ;
      > }
      > if ($empty_fields == 0) {
      > // save to database
      > }
      > ?>
      >[/color]

      hmm.. for some reason that's not working. My form is on the first page but
      I'm not going to the form, I'm attempting to go to the second page directly
      without hitting the first page at all. On the 2nd page is where the 2nd
      data is being input into the table. I put that above code just above and
      below the insert code but it's still not doing anything.

      any suggestions? thanks again in advance :)

      Comment

      • John

        #4
        Re: prevent blank data

        John <askformyemai l> wrote in news:Xns94BDDD7 46F0F1nada@216. 196.97.136:
        [color=blue]
        > Pedro Graca <hexkid@hotpop. com> wrote in
        > news:c4fc12$2gq p5j$2@ID-203069.news.uni-berlin.de:[color=green]
        >>
        >> <?php
        >> $empty_fields = 0;
        >> foreach ($_POST as $field) {
        >> if (empty($field)) ++$empty_fields ;
        >> }
        >> if ($empty_fields == 0) {
        >> // save to database
        >> }
        >> ?>
        >>[/color]
        >
        > hmm.. for some reason that's not working. My form is on the first page
        > but I'm not going to the form, I'm attempting to go to the second page
        > directly without hitting the first page at all. On the 2nd page is
        > where the 2nd data is being input into the table. I put that above
        > code just above and below the insert code but it's still not doing
        > anything.
        >
        > any suggestions? thanks again in advance :)
        >
        >[/color]


        just to follow up, i took your comments out and put in my insert code...
        that's not doing anything... was there a different way to do it? thanks
        again!

        Comment

        • Jay Moore

          #5
          Re: prevent blank data

          John wrote:[color=blue]
          > John <askformyemai l> wrote in news:Xns94BDDD7 46F0F1nada@216. 196.97.136:
          >
          >[color=green]
          >>Pedro Graca <hexkid@hotpop. com> wrote in
          >>news:c4fc12$2 gqp5j$2@ID-203069.news.uni-berlin.de:
          >>[color=darkred]
          >>><?php
          >>>$empty_field s = 0;
          >>>foreach ($_POST as $field) {
          >>> if (empty($field)) ++$empty_fields ;
          >>>}
          >>>if ($empty_fields == 0) {
          >>> // save to database
          >>>}
          >>>?>
          >>>[/color]
          >>hmm.. for some reason that's not working. My form is on the first page
          >>but I'm not going to the form, I'm attempting to go to the second page
          >>directly without hitting the first page at all. On the 2nd page is
          >>where the 2nd data is being input into the table. I put that above
          >>code just above and below the insert code but it's still not doing
          >>anything.
          >>
          >>any suggestions? thanks again in advance :)
          >>
          >>[/color]
          >
          >
          >
          > just to follow up, i took your comments out and put in my insert code...
          > that's not doing anything... was there a different way to do it? thanks
          > again!
          >[/color]
          I wrote a little class to do it:

          ---
          class chkEmpty {

          /* Variables */
          var $elements, $error, $errmsg;

          /* Constructor */

          function chkEmpty($eleme nts) {
          if (is_null($eleme nts)) {
          die("Error. Required arguments were not passed to this class.");
          }

          foreach ($elements as $key) {
          if (strlen($_POST[$key]) < 1) {
          $this->error = 1;
          $this->errmsg = "Please fill in all required fields.";
          break;
          }
          }
          } // End chkEmpty()

          } // End class chkEmpty
          ----

          Then you call it like

          $fullform = new chkEmpty(array( 'form', 'field', 'names', 'you',
          'require', 'here'))

          and then check if it returns an error

          if ($fullform->error) {
          echo $fullform->errmsg;
          } else {
          echo "A-ok!";
          }

          HTH,
          Jay

          Comment

          • John

            #6
            Re: prevent blank data

            Jay Moore <address@isp.tl d> wrote in
            news:hyNac.1050 1$YC5.10244@twi ster.rdc-kc.rr.com:
            [color=blue]
            > I wrote a little class to do it:
            >[/color]
            <complicated code snipped>

            ok wow, that is all so complicated.. I think you're overkilling this. I
            don't need it to check the form, the form is on the first page. My insert
            code is on page 2, the page that page 1 goes to when you click submit. It's
            just I'm not clicking submit this time and just going to page2 manually.
            When I do that blank info is being inserted into the table. I just want a
            way that will check on that second page that if the data being entered the
            table is blank or null then it won't be entered.

            thanks in advance if you can help with this :)

            Comment

            • Pedro Graca

              #7
              Re: prevent blank data

              John wrote:[color=blue]
              > I don't need it to check the form, the form is on the first page. My insert
              > code is on page 2, the page that page 1 goes to when you click submit. It's
              > just I'm not clicking submit this time and just going to page2 manually.[/color]

              Only when you POST (by clicking SUBMIT) is data available to page2.
              [color=blue]
              > When I do that blank info is being inserted into the table. I just want a
              > way that will check on that second page that if the data being entered the
              > table is blank or null then it won't be entered.[/color]

              <?php
              if ($_SERVER['REQUEST_METHOD '] != 'POST') {
              exit('You have to press SUBMIT for data to be available.');
              } else {
              // check for all non-empty fields
              // and save to database
              }
              --
              USENET would be a better place if everybody read: : mail address :
              http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
              http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
              http://www.expita.com/nomime.html : to 10K bytes :

              Comment

              • John

                #8
                Re: prevent blank data

                Pedro Graca <hexkid@hotpop. com> wrote in
                news:c4gjr2$2hp 46u$2@ID-203069.news.uni-berlin.de:
                [color=blue]
                > John wrote:[color=green]
                >> I don't need it to check the form, the form is on the first page. My
                >> insert code is on page 2, the page that page 1 goes to when you click
                >> submit. It's just I'm not clicking submit this time and just going to
                >> page2 manually.[/color]
                >
                > Only when you POST (by clicking SUBMIT) is data available to page2.[/color]

                Exactly, so when you go to page2 without going directly to page1 first then
                there is no data that is able to be entered.. so a blank entry is being
                reated.

                [color=blue]
                >[color=green]
                >> When I do that blank info is being inserted into the table. I just
                >> want a way that will check on that second page that if the data being
                >> entered the table is blank or null then it won't be entered.[/color]
                >
                > <?php
                > if ($_SERVER['REQUEST_METHOD '] != 'POST') {
                > exit('You have to press SUBMIT for data to be available.');
                > } else {
                > // check for all non-empty fields
                > // and save to database
                > }[/color]

                Comment

                Working...