PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Database look-up validation

  1. I purchases Gravity Forms a couple of days ago. It rocks,

    Below is a question I posed before purchasing. There was a response from Chris Hajer that said he could help me do this.

    Here is my question:
    I would like to generate a random 9 alpha and numeric codes and print them on a product. In order to enter into a contest, the buyer would enter the code. I would like your form to check and see if that 9 digit code is valid.

    can this be done? I have no database knowledge.

    Here is the link to the post that is now closed.

    Thank you,


    Posted 10 years ago on Thursday November 24, 2011 | Permalink
  2. Yes this can be done. You will have to have some database knowledge since that's where the information will be stored. It's a customization we have done in the past, but it requires you have enough knowledge to create the database table or store the values somewhere, and retrieve them.

    Please let us know how much help you're looking for (where you need to start) and we will point you in the right direction. We can't do the work for you, but we can point to existing solutions where they are helpful, or come up with a new approach for you.

    Posted 10 years ago on Thursday November 24, 2011 | Permalink
  3. I have worked with simple databases before. The table would be very simple, a set of about 1000 - 9 digit numbers. If the look up could return true (yes the number is on the list) or a false (no the number is not on the list). Then your form would make the submit button appear or have a message saying the number is not valid, like the your conditional logic feature.

    I have set up a database before on hosted site, but I had the luxury of a check list of the steps involved. Their site has information on creating SQL databases that I can follow. I think the program I used populated the database for me. But if is as simple as FTPing them, I can do that.

    I am certainly willing to do the work. If you tell me step one is create the database on, step two is populate is with your data via FTP, I think I can do that. It gets foggy after that.

    Posted 10 years ago on Thursday November 24, 2011 | Permalink
  4. Will the tables be part of your WordPress installation, a new table there, or will the values be stored in one of the existing WordPress tables? It will matter for pulling the information out.

    Can you post a simple schema for the data? Will it just be a list of 1000 unique 9 digit numbers? In a separate table but in the same database as WordPress (I think that would be easiest)? If so, we should have no problem. I have a function written that will see if the value exists in the database, and return true if it is, or a validation error if it's not.

    The basic procedure:

    • create new table in WordPress database
    • import serial numbers into database
    • create function to check value entered in form against database
    • function returns true, so continue, or false, so show "this serial number is invalid" validation error.

    Working with godaddy can be challenging sometimes. We'll do our best though. Sound good?

    Posted 10 years ago on Friday November 25, 2011 | Permalink
  5. Okay, it sounds great. I would love to do the most easy way.

    I have been researching how to create a new table in WP. Most of the text deals with creating plug-ins. I saw one mention of this " Just install a copy of phpMyAdmin in your own website space and you can do anything to the database you want." Do you think that is a safe choice? The descriptions in the things I read are over my head

    I totally understand the concepts of the procedures. I guess am am saying I need help with step 1.

    BTW, Gravity forms is amazing. I am using the conditional logic as a work around for now, but it doesn't prohibit people from making up numbers.

    This is what the numbers look like:

    Posted 10 years ago on Wednesday November 30, 2011 | Permalink
  6. Hi Chris, Any further words of wisdom on this?

    Posted 10 years ago on Thursday December 1, 2011 | Permalink
  7. Your host may have already installed phpMyAdmin or some other tool to manage the database graphically. Installing it yourself would probably not be a good idea as it requires some configuration and decisions to ensure it is secure.

    We can't help you with the design and creation of the table. Creating a new table might be a good idea, to keep it separate from your WordPress tables, but that complicates access a little bit. It seems though that your table will be very simple, just one column of values. You could even just create a plain text file and store it on your web server somewhere, where it's not publicly accessible and downloadable. That might be an easier solution.

    You could create the file with one value per line just as you have pasted.

    On a very high level, you would need to read the code that is entered. So a simple one field form to check the code. If the code is valid, continue to the next form. Or, you can check the code entered on the form with all the other information, but if it's invalid, then they wasted their time entering all the information. That part is up to you: one form or two.

    So, read the code. Compare the code to all valid codes. If there is a match, let them continue, show success, something like that. If it fails, return a validation error and let them try again.

    Does it sound like a text file will work for you, or do you want to create a database table?

    Posted 10 years ago on Friday December 2, 2011 | Permalink
  8. lets go with the text file. That seems very easy for me to upload to the site. If we go with two forms, I would imagine there would be a submit button on form 1 which then takes them to form 2 where they enter the number and we take it from there.

    Is the validation different than what we see on Gravity forms now? The question is highlight in red. Would it be different? it does't really matter. If it is easier to do two forms, lets do two.

    I have the text file ready to go. What is my next step?

    Posted 10 years ago on Friday December 2, 2011 | Permalink
  9. @briare - I will help you with this on Monday. Please send me the file, or a portion of it, so I know what kind of data we're looking it. Thanks.

    Posted 10 years ago on Sunday December 4, 2011 | Permalink
  10. Do you have a place for me to send it or do you want me to just add it to a posting?

    Posted 10 years ago on Tuesday December 6, 2011 | Permalink
  11. Hi @briare - here is some code I have modified from something I used previously.

    There are 3 pieces to it:

    lines 4-20, validate the code that is entered and return a validation error if it fails
    lines 25-35, the actual function to compare the entered code to all codes in your file
    lines 39-45, the confirmation redirect

    See if this gets you started and ask questions as you go along. Thanks.

    Posted 10 years ago on Monday December 12, 2011 | Permalink
  12. This is all very exciting, as you can tell from the time, I couldn't wait to get into it.

    Okay, question 1. line 26 how far up does the path need to be? Does it include the domain or is it until the directory where the Word Press directories are kept?

    Question 2. I am a bit confused on lines 41-44. It appears that I simple replace URL with my "success" page, but I am not 100% sure.

    Question 3. Now that I have tweaked your code to fit my form, how do I add it to the form.


    Posted 10 years ago on Monday December 12, 2011 | Permalink
  13. Q1 - I put it up only so that it is not accessible by URL. Otherwise, people could download the file and get all the serial numbers, right? So, if your website is visible at this location /home/briare/htdocs/example/index.php, you want to put it ABOVE that, so it can't be accessed at anything beginning with That's the only reason for doing it like this. If you put it in your theme folder, you can always give it a crazy long name and then just use that name in the function. Or protect it in some other way.

    My website comes up here:

    So I went up one level:

    That is not accessible by URL.

    Q2. You're right. On failure they never get to the confirmation page, so there is no issue there. When I wrote that for another project, I needed to do something crazy in the confirmation redirect, so I needed this. I think you will be fine using the form builder confirmation. You can use the query string if you need to send that 9 digit number to the next page. Just drop that last block of code altogether. Sorry for the confusion there.

    Q3. All this code goes in your theme's functions.php. You can learn where to put the code by reading here:

    Be sure the change the form ID from 70, and make sure the field is input_1 - if it's the only field in the form, it will be. Let us know if you need more help.

    Posted 10 years ago on Monday December 12, 2011 | Permalink
  14. It worked the first timeā€¦amazing. Gravity Forms just got a new promoter.

    If anything comes up, I will let you know.


    Posted 10 years ago on Monday December 12, 2011 | Permalink
  15. OK, that's AWESOME news. Please do let us know if you need anything else.

    Posted 10 years ago on Monday December 12, 2011 | Permalink

This topic has been resolved and has been closed to new replies.