PHP email form multiple field code help

Discussion in 'PHP' started by ian_ok, Jan 3, 2007.

  1. #1
    I currently use the following for validating data entered:
    function validate_fields($s) {
      $forbidden = array('\r', '\n', 'test1', 'test2', 'test3', 'test4');
      foreach ($forbidden as $f)
        if (strpos($s, $f) !== false) return false;
      return true;
    }
    
    elseif (!validate_fields($_POST['field1'])) {
      echo "Error....<br>";}
    PHP:
    So that if test1 or test2 is entered in field1 it will report an error, however I have 75 fields numbered 1-75, so is there a quicker and easier way to validate and show the error than having to repeat the elseif code for each of the 75 fields with the error message?

    Thanks Ian
     
    ian_ok, Jan 3, 2007 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    foreach $_POST as $field {
    if (!validate_fields($field)) {
      echo "Error....<br>";
    }
    PHP:
    $_POST is just an array like anything else...
     
    T0PS3O, Jan 3, 2007 IP
  3. ian_ok

    ian_ok Peon

    Messages:
    551
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Tops, but doesn't that still mean I have to do this for each field...?
    foreach $_POST as $field {
    if (!validate_fields($field)) {
      echo "Error....<br>";
    }
    
    foreach $_POST as $field1 {
    if (!validate_fields($field1)) {
      echo "Error....<br>";
    }
    foreach $_POST as $field2 {
    if (!validate_fields($field2)) {
      echo "Error....<br>";
    }
    PHP:
    and so on...?

    Ian
     
    ian_ok, Jan 3, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or if they're just numbered field1-75, it almost as easy to do just those fields instead of everything all the POST fields..

    
    for ( $x=1 ; $x<=75 ; $x++ ) {
      if ( ! validate_fields($_POST['field'.$x]) ) {
        echo "Error....<br>";
      }
    }
    
    PHP:
    EDIT: No, the foreach loop will loop through every element in the post array (each time assigning the value to the variable $field). Regardless of how the fields have been named, everything submitted via POST will be checked.
     
    rodney88, Jan 3, 2007 IP
  5. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #5
    No because they are $_POST['field1'], $_POST['field2'] etc. so by foreach-ing through $_POST you catch them all no matter whether there are 5 or 5000.

    You'll probably want to slightly change my code because now you potentially get 75 error messages, it could break after one for example.

    Rodney's suggestion works too but hard codes the number of fields. Which you could get round with sizeof() but anyway, there's always more than one way.
     
    T0PS3O, Jan 3, 2007 IP