Search Drupal’s Database from a Node

This is an interesting way to search Drupal 7 tables from a node using user form input. Theoretically, you really should write a module if you want to do this kind of thing. This method is less secure. It is just for demonstration. PHP needs to be enabled for the administrator in Drupal.

First, you need some javascript and ajax. This can be placed in the node.

 

Next, you need some kind of form in the node. Here is a simple form to get user input.

After the user submits the form, the input will need to be sanitized. A PHP file “check.php” is opened through the ajax request (shown in the script above) to handle this. If you have a place where you keep custom scripts, you could place the “check.php” there. Be sure to adjust the address to it in the javascript above.

Along with sanitizing the input, “check.php” also connects to the database, runs the query, and outputs the results.

There are much more sophisticated and secure (Drupal database abstraction) ways to run database queries in Drupal, but this is kind of interesting.