0

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->fetchAssoc()) {
    echo '<script type="text/javascript">';
    echo 'window.onload(';
    echo 'document.getElementById("pathfinder-primary-form").innerHTML=';
    echo '"<div>';
    echo $row['title'];
    echo '</div>");';
    echo '</script>';
  }
  }

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->fetchAssoc()) {
    $title = $row['title'];
    echo '<p>' . $title. '</p>';

  }
  }

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?

1
  • 1
    Drupal has a theme system, the need to output directly from a function is pretty rare. The difference between outputting within a form, and getting data from a form function to an unrelated element somewhere else on the page, is a pretty big one. Render arrays and template preprocess vars would also be good to get your head around before going any further
    – Clive
    Commented Jan 15, 2015 at 23:37

1 Answer 1

1

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' => '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->fetchAssoc()) {
      $title = $row['title'];
      $output.= '<p>' . $title. '</p>';

    }
  }
  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 = '';

6
  • Brilliant! Thank you Maarten, that's exactly what I was looking for. You know I had been thinking after I posted that it might have something to do with 'return' results and played around with it using the code I had but it just gave me errors. Obviously. I was missing the hook_menu() which is all new to me. And exciting. Thanks for introducing me to this and spelling this out. Very awesome :)
    – Sage
    Commented Jan 17, 2015 at 9:38
  • The main problem I'm having with this code is that my php rejects return $output as 'Fatal error: Unsupported operand type'. When I remove that line it gives me no error (although it also displays nothing of course). Any idea what might be going on there?
    – Sage
    Commented Jan 20, 2015 at 4:41
  • Could you post your code? Commented Jan 20, 2015 at 10:48
  • function pathfinder_form_complete($form, &$form_state) { $output = ' '; $result = db_query("SELECT nid, type, title FROM {node} WHERE type = :type", array(':type' => 'resource',)); if ($result) { while ($row = $result->fetchAssoc()) { $title = $row['title']; $output .= $title; } } return $output; }
    – Sage
    Commented Jan 20, 2015 at 18:19
  • I see you are returning output after a form submit. That's a little different than a regular page. Check out this example: drupal.stackexchange.com/questions/54374/… Commented Jan 21, 2015 at 6:39

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.