Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts

    POST data validation help

    Hi everyone, im not sure where to look or what to look for on this one.

    I want to make sure all fields are filled and if not then they are ignored or alerted, would this be better with js?

    example, if the last of each textbox is empty it would alert me or ignore instead of just passing an empty post data.

    Code:
    <input type="text" id="C0" name="C_0[]" value="entered 1" />
    <input type="text" id="C0" name="C_0[]" value="entered 1" />
    <input type="text" id="C0" name="C_0[]" value="" />
    
    <input type="text" id="C1" name="C_1[]" value="entered 2" />
    <input type="text" id="C1" name="C_1[]" value="entered 2" />
    <input type="text" id="C1" name="C_1[]" value="" />
    
    <input type="text" id="C2" name="C_2[]" value="entered 3" />
    <input type="text" id="C2" name="C_2[]" value="entered 3" />
    <input type="text" id="C2" name="C_2[]" value="" />
    any help or direction would be appreciated.

  • #2
    New Coder
    Join Date
    Nov 2010
    Location
    United Kingdom
    Posts
    31
    Thanks
    0
    Thanked 9 Times in 8 Posts
    u can use js, but then u also will have to check if js is not disabled on user pc. Also i would recomend u to use php validation.

  • #3
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    well i know i can use js, im asking how to validate a form with names like that.....

  • #4
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    In simple terms, each input box would be referenced like so:

    PHP Code:
    $_POST['C_0'][0]
    $_POST['C_0'][1]
    $_POST['C_0'][2]


    $_POST['C_1'][0]
    $_POST['C_1'][1]
    $_POST['C_1'][2]

    $_POST['C_2'][0]
    $_POST['C_2'][1]
    $_POST['C_2'][2
    And...

    PHP Code:
    if (!@$_POST['C_1'][0]) {
      echo(
    'C_1 index 0 was empty');

    Beeeecause... An empty or unset item converts to false in PHP. The @ silences any notices about "x is not set"... This catches both empty ("") and unset variables.
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #5
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    ok that helps, so would i have to make a

    PHP Code:
    if (!@$_POST['C_1'][0]) {
      echo(
    'C_1 index 0 was empty');

    for each text field then?

  • #6
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    Depends how clever you want to be...

    PHP Code:
    for($c=0$c<3$c++) {
      for (
    $i=0$i<3$i++) {
        if (!@
    $_POST['C_'.$c][$i]) {
          echo(
    'C_'.$c.' index '.$i.' was empty');
        }
      }

    On the cunning scale, that code is slightly under "Fox".
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog

  • #7
    Regular Coder seco's Avatar
    Join Date
    Nov 2008
    Location
    Oregon
    Posts
    687
    Thanks
    6
    Thanked 79 Times in 77 Posts
    I guess the easiest thing to do is show an array from post data, the text field amount will be different every time, could be 3 per array or it could be 100. I want to check if anything doesnt have a value and return a error.

    Code:
    Array
    (
        [title] => test
        [C_0] => Array
            (
                [0] => 111
                [1] => 111
                [2] => 111
                [3] => 
                [4] => 111
                [5] => 111
                [6] => 111
            )
    
        [C_1] => Array
            (
                [0] => 222
                [1] => 222
                [2] => 222
                [3] => 222
                [4] => 222
                [5] => 
                [6] => 222
            )
    
        [C_2] => Array
            (
                [0] => 333
                [1] => 333
                [2] => 333
                [3] => 333
                [4] => 333
                [5] => 333
                [6] => 
            )
    
    )
    Last edited by seco; 12-06-2010 at 01:58 AM.

  • #8
    Super Moderator
    Join Date
    Feb 2009
    Location
    England
    Posts
    539
    Thanks
    8
    Thanked 63 Times in 54 Posts
    A variable number of array items will want some research. I haven't tested this, but I expect if one is empty, it'll renumber the array and not do what you want. Perhaps naming them <input name="C_0[0]"> etc would be better. PHP *should* take the index from that, but I've never tried.

    This also makes the code rather difficult. Without putting too much thought into it, you might need to run a foreach on the arrays and find the highest index number, then use that in the for loop. Like:

    PHP Code:
    $current_max 0;
    foreach(
    $_POST['C_0'] as $key => $value) {
      
    $current_max max($key$current_max);
    }

    for(
    $c=0$c<$current_max$c++)... 
    Now do the same sort of thing for $i, finding the highest index.

    In short, you'll have to do a little research on how the browser and PHP handles an empty array entry in the middle, or end. Make the last entry empty and var_dump() it - see if the last entry actually appears in the array. If it doesn't, you'll have to set <input type="hidden" name="C_0_max" value="10" /> for C_0 having 10 entries in the HTML, and use that as $current_max...
    lamped.co.uk :: Design, Development & Hosting
    marcgray.co.uk :: Technical blog


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •