Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2012
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.34 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/database-look-up-validation
  2. // validate 7 digit code
  3. // change the 3 here to your form ID
  4. add_filter('gform_validation_3', '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($csvdata, $id, $_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 ABC ID you entered is invalid: please try again.';
  15.                 break;
  16.             }
  17.         }
  18.     }
  19.     return $validation_result;
  20. }
  21.  
  22.  
  23. // This creates the array from the data file in the fuction below
  24. function csv_in_array($url,$delm=";",$encl="\"",$head=false) {
  25.    
  26.     $csvxrow = file($url);   // ---- csv rows to array ----
  27.    
  28.     $csvxrow[0] = chop($csvxrow[0]);
  29.     $csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
  30.     $keydata = explode($delm,$csvxrow[0]);
  31.     $keynumb = count($keydata);
  32.    
  33.     if ($head === true) {
  34.     $anzdata = count($csvxrow);
  35.     $z=0;
  36.     for($x=1; $x<$anzdata; $x++) {
  37.         $csvxrow[$x] = chop($csvxrow[$x]);
  38.         $csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
  39.         $csv_data[$x] = explode($delm,$csvxrow[$x]);
  40.         $i=0;
  41.         foreach($keydata as $key) {
  42.             $out[$z][$key] = $csv_data[$x][$i];
  43.             $i++;
  44.             }    
  45.         $z++;
  46.         }
  47.     }
  48.     else {
  49.         $i=0;
  50.         foreach($csvxrow as $item) {
  51.             $item = chop($item);
  52.             $item = str_replace($encl,'',$item);
  53.             $csv_data = explode($delm,$item);
  54.             for ($y=0; $y<$keynumb; $y++) {
  55.                $out[$i][$y] = $csv_data[$y];
  56.             }
  57.         $i++;
  58.         }
  59.     }
  60.  
  61. return $out;
  62. }
  63.  
  64. // This calls the abcid.txt file and sends it to the processor function above
  65. $csvdata = csv_in_array( "/home/abcid.txt", ";", "\"", true );
  66.  
  67.  
  68.  
  69. // This searches the array also making another array of just the abcid values
  70. $id = 'abcid';
  71. function is_code_valid($csvdata, $id, $thiscode) {
  72.     $results = array();
  73.     is_code_valid_r($array, $key, $value, $results);
  74.     if($thiscode == $results[0]['#value']) {
  75.         return TRUE;
  76.     }
  77.     // if we did not have a match and are out of codes, return FALSE
  78.     return FALSE;
  79. }
  80.  
  81. // This is the secondary array
  82. function is_code_valid_r($array, $key, $value, &$results) {
  83.     if (!is_array($array))
  84.         return;
  85.  
  86.     if ($array[$key] == $value)
  87.         $results[] = $array;
  88.  
  89.     foreach ($array as $subarray)
  90.         is_code_valid_r($subarray, $key, $value, $results);
  91. }
  92.  
  93.  
  94. // doing this here because the redirect URL does not support variables or shortcodes
  95. // change the 3 here to your form ID
  96. add_filter('gform_confirmation_3', 'valid_invitation_confirmation', 10, 4);
  97. function valid_invitation_confirmation($confirmation, $form, $lead, $ajax){
  98.     // customize this URL - I send the code in the query string
  99.     $success_url = 'www.example.com/registration-step-2/?abcid=' . $lead[1];
  100.     $confirmation = array('redirect' => $success_url);
  101.     return $confirmation;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement