Input error checking on arrays..

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

    Input error checking on arrays..

    If I want to check for input of an integer I've got the following (I
    get the form input with $input = "$_POST[input]"):

    if(!ereg("^[0-9]+$",$_POST[input])) {
    echo "Input is incomplete or incorrect.";
    }

    If, instead of only getting one 'input' I wanted to get n instances of
    input, I'd generate input fields for each of n instances I want in a
    for loop, then get the input with:

    $input[$cnt] = $_POST["input"][$cnt];

    Of course, then if I've got the following:

    if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
    echo "Input is incomplete or incorrect.";
    }

    I'll get the error output if or if not the input was correct or as
    intended (in this case integer(s) only). It's clearly an issue with
    either the formatting of the ereg condition, or with getting the input
    data itself (I'm not certain which). I've had a few ideas and changed
    a few things, included a few others, etc.; but I've gotten nowhere
    with it on my own yet and instead of wasting others' time with giving
    those incorrect examples on here I'd be open to (and grateful for)
    insights on what I ought to do in order to get this to work.

    Thank you,

    - Oeln
  • Jan Pieter Kunst

    #2
    Re: Input error checking on arrays..

    In article <ffde43bc.04040 90315.3ea4ede9@ posting.google. com>,
    ohmy9od@yahoo.c om (Oeln) wrote:
    [color=blue]
    > $input[$cnt] = $_POST["input"][$cnt];
    >
    > Of course, then if I've got the following:
    >
    > if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
    > echo "Input is incomplete or incorrect.";
    > }[/color]

    It's unclear what exactly $cnt is supposed to be or where it gets its
    value from. Try do a:

    echo '<pre>';
    print_r($_POST) ;
    echo '</pre>';

    To see what $_POST looks like after submitting. Is $_POST['input']
    actually an array? What is the value of $cnt? Does $_POST['input'] have
    a key that is equal to the value of $cnt?

    JP

    --
    Sorry, <devnull@cauce. org> is een "spam trap".
    E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

    Comment

    • Oeln

      #3
      Re: Input error checking on arrays..

      Jan Pieter Kunst <devnull@cauce. org> wrote in message news:<devnull-7EEE43.13243109 042004@news1.ne ws.xs4all.nl>.. .[color=blue]
      > In article <ffde43bc.04040 90315.3ea4ede9@ posting.google. com>,
      > ohmy9od@yahoo.c om (Oeln) wrote:
      >[color=green]
      > > $input[$cnt] = $_POST["input"][$cnt];
      > >
      > > Of course, then if I've got the following:
      > >
      > > if(!ereg("^[0-9]+$",$_POST["input"][$cnt])) {
      > > echo "Input is incomplete or incorrect.";
      > > }[/color]
      >
      > It's unclear what exactly $cnt is supposed to be or where it gets its
      > value from. Try do a:
      >
      > echo '<pre>';
      > print_r($_POST) ;
      > echo '</pre>';
      >
      > To see what $_POST looks like after submitting. Is $_POST['input']
      > actually an array? What is the value of $cnt? Does $_POST['input'] have
      > a key that is equal to the value of $cnt?
      >
      > JP[/color]

      Yes, initially I would get input indicating the number of 'inputs' one
      wants to be offered ($input_cnt, for instance). On the next form, I
      get

      $input_cnt = "$_POST[input_cnt]";

      In a for loop, I offer $input_cnt number of 'inputs':

      for($cnt=0; $cnt<$input_cnt ; $cnt++) {
      echo "<input type=\"text\" name=\"input\"> ";
      }

      I include <input type"hidden" name="input_cnt " value="$input_c nt"> in
      this form, too.

      On the next (i.e., third) form, I want to get the data for each of the
      inputs:

      for($cnt=0; $cnt<$input_cnt ; $cnt++) {
      $input[$cnt] = $_POST["input"][$cnt];
      }

      I want to check the input for each with ereg (in this case I only want
      integers, for example)..

      - Oeln

      Comment

      • Ian.H

        #4
        Re: Input error checking on arrays..

        On Fri, 09 Apr 2004 11:42:20 -0700, Oeln wrote:
        [color=blue]
        > Yes, initially I would get input indicating the number of 'inputs' one
        > wants to be offered ($input_cnt, for instance). On the next form, I
        > get
        >
        > $input_cnt = "$_POST[input_cnt]";[/color]
        ^ ^

        Better written (IMO):

        $input_cnt = $_POST['input_cnt'];


        [color=blue]
        >
        > In a for loop, I offer $input_cnt number of 'inputs':
        >
        > for($cnt=0; $cnt<$input_cnt ; $cnt++) {
        > echo "<input type=\"text\" name=\"input\"> ";
        > }[/color]


        And you've just severely broken multiple fields. They'd all be called
        'input' so if you $cnt == 10.. only the first value will be used.

        Single quotes would be netter here also:


        echo '<input type="text" name="input">';

        (but 'input' never increments for submission purposes).

        [color=blue]
        >
        > I include <input type"hidden" name="input_cnt " value="$input_c nt"> in
        > this form, too.
        >
        > On the next (i.e., third) form, I want to get the data for each of the
        > inputs:
        >
        > for($cnt=0; $cnt<$input_cnt ; $cnt++) {
        > $input[$cnt] = $_POST["input"][$cnt];
        > }
        >
        > I want to check the input for each with ereg (in this case I only want
        > integers, for example)..[/color]


        is_int() should perform this part for you.



        Regards,

        Ian

        --
        Ian.H
        digiServ Network
        London, UK


        Comment

        • Jan Pieter Kunst

          #5
          Re: Input error checking on arrays..

          In article <ffde43bc.04040 91042.7cca2ee3@ posting.google. com>,
          ohmy9od@yahoo.c om (Oeln) wrote:
          [color=blue]
          > In a for loop, I offer $input_cnt number of 'inputs':
          >
          > for($cnt=0; $cnt<$input_cnt ; $cnt++) {
          > echo "<input type=\"text\" name=\"input\"> ";
          > }
          >
          > I include <input type"hidden" name="input_cnt " value="$input_c nt"> in
          > this form, too.[/color]

          I see. The problem here is that you don't create an array of "input".
          The last (or first, not sure) of the fields will 'win' and you end up
          with just one value in $_POST, not an array.

          You could do it like this:

          for($cnt=0; $cnt<$input_cnt ; $cnt++) {
          echo "<input type=\"text\" name=\"input[]\">";
          }

          Note [] after the "name": that gives you a numeric array called "input".
          Also, it's not necessary to put the "count" of that array in an hidden
          variable. Arrays know their size.

          Upon receiving the submitted page, you can now do:

          foreach($_POST['input'] as $input) {
          check_for_desir ed_characterist ics($input);
          }

          HTH,
          JP

          --
          Sorry, <devnull@cauce. org> is een "spam trap".
          E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

          Comment

          • Oeln

            #6
            Re: Input error checking on arrays..

            Jan Pieter Kunst <devnull@cauce. org> wrote in message news:<devnull-5259F5.00351410 042004@news1.ne ws.xs4all.nl>.. .[color=blue]
            > In article <ffde43bc.04040 91042.7cca2ee3@ posting.google. com>,
            > ohmy9od@yahoo.c om (Oeln) wrote:
            >[color=green]
            > > In a for loop, I offer $input_cnt number of 'inputs':
            > >
            > > for($cnt=0; $cnt<$input_cnt ; $cnt++) {
            > > echo "<input type=\"text\" name=\"input\"> ";
            > > }
            > >
            > > I include <input type"hidden" name="input_cnt " value="$input_c nt"> in
            > > this form, too.[/color]
            >
            > I see. The problem here is that you don't create an array of "input".
            > The last (or first, not sure) of the fields will 'win' and you end up
            > with just one value in $_POST, not an array.
            >
            > You could do it like this:
            >
            > for($cnt=0; $cnt<$input_cnt ; $cnt++) {
            > echo "<input type=\"text\" name=\"input[]\">";
            > }
            >
            > Note [] after the "name": that gives you a numeric array called "input".
            > Also, it's not necessary to put the "count" of that array in an hidden
            > variable. Arrays know their size.
            >
            > Upon receiving the submitted page, you can now do:
            >
            > foreach($_POST['input'] as $input) {
            > check_for_desir ed_characterist ics($input);
            > }
            >
            > HTH,
            > JP[/color]

            Oh, I'm an idiot - I've got the '[]'. I forgot to include that; but
            it's in there. I've got no issue getting the data itself, it's only
            checking the input with the ereg condition in this case which I've got
            an issue with.. ;/

            Comment

            • Oeln

              #7
              Re: Input error checking on arrays..

              In order to illustrate the idea I've got, I'll offer the following:

              if(
              foreach($_POST[input]) {
              (!ereg("^[0-9]+$",$_POST[input]))
              }) {
              include('input_ errchk.inc');
              } else { ..

              Inside an if loop, I want to figure out if the input is *not* as
              intended (i.e., as indicated in the ereg condition). If it, or if one
              of them, is not, I want to include a certain file which outputs an
              error indicating this fact; otherwise, continue onward..

              I'm not certain if I can "if(foreach (.." etc. - I get the impression
              it oughtta work; but I'm only getting an error with it. (I've of
              course changed this a few times to include certain things in case I'm
              only a little bit off - including $_POST['input'] or $_POST["input"]
              instead of $_POST[input], $_POST[input] as $input, etc.)

              It would be ideal if instead of even going with a foreach loop, I
              could compare the array itself to an ereg condition instead of each of
              the individual 'inputs' in the array (I'm only not certain what to
              include in this, or if it's an option). I'd want to indicate the array
              could include integers only, and whatever is in between them in the
              array in order to isolate one from the other. It's only got to include
              one 'input', but it could include 10, for instance. I'd imagine this
              would be like "^[0-9]+" for at least one, then "[0-9]*$" for the
              others, but I'm not certain what else I'd include in the ereg
              condition itself..

              - Oeln

              Comment

              • Jan Pieter Kunst

                #8
                Re: Input error checking on arrays..

                In article <ffde43bc.04040 92042.1e628a@po sting.google.co m>,
                ohmy9od@yahoo.c om (Oeln) wrote:
                [color=blue]
                > I'm not certain if I can "if(foreach (.." etc. - I get the impression
                > it oughtta work; but I'm only getting an error with it. (I've of
                > course changed this a few times to include certain things in case I'm
                > only a little bit off - including $_POST['input'] or $_POST["input"]
                > instead of $_POST[input], $_POST[input] as $input, etc.)
                >
                > It would be ideal if instead of even going with a foreach loop, I
                > could compare the array itself to an ereg condition instead of each of
                > the individual 'inputs' in the array (I'm only not certain what to
                > include in this, or if it's an option).[/color]

                No, that's an impossible construct you are trying to make. If you want
                to check every member of the array for certain characteristics , the
                "foreach" has to be outermost and the check with its "if" has to be
                inside that foreach loop.

                And you can't check every member of an array without looping through the
                array.

                See here for basic info:

                <http://www.php.net/manual/en/control-structures.php>

                JP

                --
                Sorry, <devnull@cauce. org> is een "spam trap".
                E-mail adres is <jpk"at"akamail .com>, waarbij "at" = @.

                Comment

                • Oeln

                  #9
                  Re: Input error checking on arrays..

                  Jan Pieter Kunst <devnull@cauce. org> wrote in message news:<devnull-9B7226.07372910 042004@news1.ne ws.xs4all.nl>.. .[color=blue]
                  > In article <ffde43bc.04040 92042.1e628a@po sting.google.co m>,
                  > ohmy9od@yahoo.c om (Oeln) wrote:
                  >[color=green]
                  > > I'm not certain if I can "if(foreach (.." etc. - I get the impression
                  > > it oughtta work; but I'm only getting an error with it. (I've of
                  > > course changed this a few times to include certain things in case I'm
                  > > only a little bit off - including $_POST['input'] or $_POST["input"]
                  > > instead of $_POST[input], $_POST[input] as $input, etc.)
                  > >
                  > > It would be ideal if instead of even going with a foreach loop, I
                  > > could compare the array itself to an ereg condition instead of each of
                  > > the individual 'inputs' in the array (I'm only not certain what to
                  > > include in this, or if it's an option).[/color]
                  >
                  > No, that's an impossible construct you are trying to make. If you want
                  > to check every member of the array for certain characteristics , the
                  > "foreach" has to be outermost and the check with its "if" has to be
                  > inside that foreach loop.
                  >
                  > And you can't check every member of an array without looping through the
                  > array.
                  >
                  > See here for basic info:
                  >
                  > <http://www.php.net/manual/en/control-structures.php>
                  >
                  > JP[/color]

                  Okay, well I've figured out a way to get it to work. I initially get
                  the input data in a for loop, then compare with the intended ereg
                  condition. If it fails once, $input_errchk = 1

                  I've then got an if loop where if($input_errch k) {
                  include('input_ errchk.inc'); die; ..in other words, I include the
                  input error output file I've got (in which I indicate what the error
                  is) instead of whatever else I would output, etc. etc.

                  Thanks for the input on this, Jan.

                  - Oeln

                  Comment

                  Working...