Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // http://www.gravityhelp.com/forums/topic/wedding-rsvp-dynamic-population
- // originally http://www.gravityhelp.com/forums/topic/database-look-up-validation
- // validate a code entered in field 1 of your form with a text file
- // change the 2 here to your form ID
- add_filter('gform_validation_2', 'validate_code');
- function validate_code($validation_result){
- // this assumes the code is entered in field one on your form
- // change this input_ number if it's a different field
- // is_code_valid function can be found below. This calls the function and if FALSE is returned, a validation error is returned
- if(!is_code_valid($_POST['input_1'])){
- $validation_result['is_valid'] = false;
- foreach($validation_result['form']['fields'] as &$field){
- // field 1 is the field where we want to show the validation message
- if($field['id'] == 1){
- $field['failed_validation'] = true;
- $field['validation_message'] = 'The code you entered is invalid: please try again.';
- break;
- }
- }
- }
- return $validation_result;
- }
- // use this function to validate the entered code
- // called from the validate_code function above
- function is_code_valid($thiscode){
- // read all the codes in from the guests.txt file
- // change the name and location of this file
- /* here is the format of the file
- 12345,Chris Hajer,Abraham Lincoln
- 56789,Phillipo Berrio,Ron Paul,George Washington
- 222,Don King,Jesse Ventura,Jeb Bush,Barack Obama
- 5566,The Situation
- */
- $codes = file('/home/content/html/website/guests.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
- // read each line and split the line into the $codes array, on commas
- foreach($codes as $code){
- $guests = explode (",", $code);
- // compare the entered code to all codes in the file until we find a match
- // the [0] indexed item is our code
- if($thiscode == $guests[0]){
- // the entered code matched, so grab all the names AND return true
- // the populate_guest_names function can be found below
- populate_guest_names($guests);
- // return true because this is a valid code
- return TRUE;
- }
- }
- // if we did not have a match and are out of codes, return FALSE
- return FALSE;
- }
- // populate all the guest names that are associated with this password
- // this function accepts an array which is the code and all the names associated with it
- // called from the is_code_valid function above
- function populate_guest_names($names){
- // count the number of guests associated with this code
- // subtract 1 so we don't count the password as an array item
- $number_of_names = count($names)-1;
- // the functions below can be simplified and don't account for any number of guests less than 1 or more than 4
- if ($number_of_names == 1) {
- // split the $names[1] array item on the space between first and last name
- $guest1 = explode (" ",$names[1]);
- // assign the first and last name to the $_POST to populate hidden inputs in the form
- $_POST['input_2'] = $guest1[0];
- $_POST['input_3'] = $guest1[1];
- } elseif ($number_of_names == 2) {
- // repeat the same process for 2, 3 or 4 names, just assigning more variables
- $guest1 = explode (" ",$names[1]);
- $_POST['input_2'] = $guest1[0];
- $_POST['input_3'] = $guest1[1];
- $guest2 = explode (" ",$names[2]);
- $_POST['input_4'] = $guest2[0];
- $_POST['input_5'] = $guest2[1];
- } elseif ($number_of_names == 3) {
- $guest1 = explode (" ",$names[1]);
- $_POST['input_2'] = $guest1[0];
- $_POST['input_3'] = $guest1[1];
- $guest2 = explode (" ",$names[2]);
- $_POST['input_4'] = $guest2[0];
- $_POST['input_5'] = $guest2[1];
- $guest3 = explode (" ",$names[3]);
- $_POST['input_6'] = $guest3[0];
- $_POST['input_7'] = $guest3[1];
- } else {
- $guest1 = explode (" ",$names[1]);
- $_POST['input_2'] = $guest1[0];
- $_POST['input_3'] = $guest1[1];
- $guest2 = explode (" ",$names[2]);
- $_POST['input_4'] = $guest2[0];
- $_POST['input_5'] = $guest2[1];
- $guest3 = explode (" ",$names[3]);
- $_POST['input_6'] = $guest3[0];
- $_POST['input_7'] = $guest3[1];
- $guest4 = explode (" ",$names[4]);
- $_POST['input_8'] = $guest4[0];
- $_POST['input_9'] = $guest4[1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement