Get city population after user's city is entered? [RESOLVED]

Hello everyone!

I am working for a home loans company in Canada, but basically we don’t service very small towns.

I want to create some conditional logic that will show a rejection message if the person has a input a small town as their address. I can probably make a very long conditional that includes every smaller town, but not sure if this is the best way to do things.

Any tips and advice?

Hi Jonathan. How are you going to maintain the information that determines if you service them or not? Is that in a file, or a database, or an external service? How many towns are in the Yes list and how many are in the No list? There are several ways to approach this. You will most likely be looking at using gform_field_validation:

Hi Chris!

I was going to store a json/csv file on the website. About 100 towns in both the yes and no lists.

OK. I think in that case I recommend using gform_field_validation. You can read that file (either CSV or JSON) using PHP and this filter:

Then you can return a validation error (message if they are on the No list) or let them pass to the next page if they are on the Yes list. There is a possibility they are not on either list, and I am not sure what you want to return to them in that case.

We covered doing something like that here:

and

Take a look at those topics and let us know if you need a nudge in the right direction.

Hi Chris,

Thanks for the help here.
I still want the user to submit an email to us if they are in a “bad” city so we can review the file further.

From what I understand this validation rule will stop the user from filling out the form.

Additionally, can I fetch the population from the data file and put it into the Gravity Form for use in conditional logic?

Yes, you could populate fields in the form with additional information, and then use conditional logic to route them somewhere else, rather than returning an error. I think I would use gform_pre_submission in that case, so that you can retrieve the data from the data file, and then populate hidden fields in the form with data you retrieved. Then you can show one confirmation or another (based on the data you retrieved) using conditional logic. gform_field_validation would be used to stop them from submitting the form and return an error, which it does not sound like you want.

1 Like

Hi Jonathan (@user5f15f943c6034976),

Since you want to still allow the user to submit the form, how about showing an HTML field with your rejection message via conditional logic if one of your 100 small towns is entered?

Populate Anything makes this a breeze. It doesn’t support json/csv files so you’ll need to either import the data as a custom table in your WordPress database - or - just use Gravity Forms itself as a simple database.

Once you have the data accessible to Populate Anything, you could create a Hidden field whose value is dynamically populated based on the value of another field. You could label this field “Is small town?” When the user enters their town, you can filter the value of this Hidden field to return a true or false depending on whether the town name exists in your list of small towns.

Here’s an example of what that field might look like if you use a Gravity Form to store your small town list:

Then, you can show/hide your HTML field based on the value of this “Is small town?” field.

1 Like

Hi David!

Thanks for your input here.
What I ended up doing is installing a plugin for Custom Javascript and then importing the json and pasting it into the fields. I think your method is probably a better way to go though.