chrishajer

validate 9 digit code before sending visitor on

Dec 12th, 2011
1,911
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // http://www.gravityhelp.com/forums/topic/database-look-up-validation
  2. // validate 9 digit code
  3. // change the 70 here to your form ID
  4. add_filter('gform_validation_70', 'validate_code');
  5. function validate_code($validation_result){
  6.     // this assumes the code is entered in field one on your form
  7.     // change this input_ number if it's a different field
  8.     if(!is_code_valid($_POST['input_1'])){
  9.         $validation_result['is_valid'] = false;
  10.         foreach($validation_result['form']['fields'] as &$field){
  11.         // field 1 is the field where we want to show the validation message
  12.             if($field['id'] == 1){
  13.                 $field['failed_validation'] = true;
  14.                 $field['validation_message'] = 'The code you entered is invalid: please try again.';
  15.                 break;
  16.             }
  17.         }
  18.     }
  19.     return $validation_result;
  20. }
  21.  
  22. // use this function to validate codes
  23. function is_code_valid($thiscode){
  24.     // read all the codes in from the numbers.txt file
  25.     // change the path here to the location of your file
  26.     $codes = file('/kunden/homepages/99/d12345678/htdocs/numbers.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  27.     foreach($codes as $code){
  28.         // compare the entered code to all codes in the file until we find a match
  29.         if($thiscode == $code){
  30.             return TRUE;
  31.         }
  32.     }
  33.     // if we did not have a match and are out of codes, return FALSE
  34.     return FALSE;
  35. }
  36.  
  37. // doing this here because the redirect URL does not support variables or shortcodes
  38. // change the 70 here to your form ID
  39. add_filter('gform_confirmation_70', 'valid_invitation_confirmation', 10, 4);
  40. function valid_invitation_confirmation($confirmation, $form, $lead, $ajax){
  41.     // customize this URL - I send the code in the query string
  42.     $success_url = get_bloginfo('url') . '/?code=' . $lead[1];
  43.     $confirmation = array('redirect' => $success_url);
  44.     return $confirmation;
  45. }
RAW Paste Data