Displaying Database Data Within the Flow the Site I have a multi-page form that works fine in and of itself (pathfindertool.module). However, once a user clicks submit I want the page to display some records on the page. And it DOES display the records on the page, but NOT within the flow of the page. What I mean by that is, it just puts all the returned results in the top-left of the page in the browser window in  and not in a  tag in the #content area for example where content is supposed to go.

I tried to use javascript document.getelementbyid.innerhtml, calling that from my php and it just returns no results (well it returned them according to console but it didn't display them):

while ($row = $result-&gt;fetchAssoc()) {
    echo '&lt;script type="text/javascript"&gt;';
    echo 'window.onload(';
    echo 'document.getElementById("pathfinder-primary-form").innerHTML=';
    echo '"&lt;div&gt;';
    echo $row['title'];
    echo '&lt;/div&gt;");';
    echo '&lt;/script&gt;';
  }
  }


I also tried doing drupal_add_js inside my .module file and can't get that to work either. Granted my coding here is pretty kloogy:

drupal_add_js('document.getelementbyId("pathfinder-primary-form").innerHTML = "', 'inline');

$title = $row['title'];

drupal_add_js('document.getelementbyId("pathfinder-primary-form").innerHTML = "', 'inline');


Surely this must be one of the most common workflows ever. What's the best practice for displaying data from a database in Drupal so that it displays within a specific  (ie. #main, or perhaps even the form container itself)?

Here's my pretty basic functioning code so far for displaying the results in my .module file:

if ($result) {
while ($row = $result-&gt;fetchAssoc()) {
    $title = $row['title'];
    echo '&lt;p&gt;' . $title. '&lt;/p&gt;';

  }
  }


But of course it displays it in the top-left corner of the body in the browser window right above the Logo! O.O

What am I doing wrong and how can I fix it?
Drupal splits functionality (modules) and layout (themes) to keep everything maintainable.

Instead of echoing everything, you need to pass your output from your module to the theme.

First step is making a menu item. This will give you an url to work with: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_menu/7

Basic example:

function pathfindertool_hook_menu() {
    $items['pathfinderurl'] = array(
      'page callback' =&gt; 'pathfindertool_page',
    );
    return $items;
}


The page callback point to a function that contains your logic. You can choose the function name, but it's advised to start with your module name.

function pathfindertool_page() {
  $output = '';
  drupal_add_js('document.getelementbyId("pathfinder-primary-form").innerHTML = "', 'inline');

  $title = $row['title'];

  drupal_add_js('document.getelementbyId("pathfinder-primary-form").innerHTML = "', 'inline');

  // Do whatever you did to fill result.
  if ($result) {
    while ($row = $result-&gt;fetchAssoc()) {
      $title = $row['title'];
      $output.= '&lt;p&gt;' . $title. '&lt;/p&gt;';

    }
  }
  return $output;
}


Returning the output will make sure it will show in the right place. Never use echo or print, it will show up at the top.

You must always return something, even an empty string is ok, return NULL will result in a WSOD. That's why I always start my function with $output = '';