0

What might be a sane approach to prepopulating web form fields using data from an external database on the fly? This is my scenario: there is a form that we need clients to fill out, but we already have a lot of the client's information in our database. So to make it easier on the client, we want to fill out the form as much as possible before sending it to them to fill out the rest. I'm using the webform module on our Drupal 7 site, but all the data needs to come from our other database. Ideally, there would be either a page or just a field where I can select the client ID. After choosing the client ID, other fields would use that ID to pull information like contact name, address, etc. Is there a reasonable way to do this?

2 Answers 2

0

You would need to expose an API from your external database. If the external database is also a Drupal 7 site, you could use the RestWS module to do this (https://www.drupal.org/project/restws).

Once you have your data exposed via api, you would then have to connect to the api on the client's site, and you could auto populate some hidden fields via an ajax call when they select the ID. You would want to do this in a custom module.

3
  • That seems logical. Is this approach still possible if I wanted to query the database directly instead of exposing an API (which doesn't exist)?
    – linucksrox
    Sep 1, 2016 at 13:30
  • I've never queried a database from an external site. Not sure how you'd do that. I think the API would be safer. Sep 7, 2016 at 19:38
  • Since our database is hosted on the same network, I can directly connect to that database by adding the credentials to settings.php, then using db_set_active('mydatabase'), running queries, and finally going back to drupal's database using db_set_active(). I'm in the process of writing a module that will override some data in specific forms, and hope to post my solution when I get it fully working. Thanks for pointing me in the right direction!
    – linucksrox
    Sep 9, 2016 at 15:31
0

I ended up figuring out what to do (thanks bgrobertson). I decided to post the information on my blog instead of just putting it here.

Connecting To External Databases In Drupal 7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.