I'm trying to get a D7 form into a Bootstrap modal dialog. I had this working on D6, through a semi-straightforward process:

  • The jQuery code that's building and showing the modal does an $.ajax call to a url on my site to get the form contents -- something like /get_my_form/123. This request is picked up by a Drupal menu entry that makes a call to drupal_get_form() with the appropriate form function and various parameters. The usual Drupal form-creation thing.

  • There's a theme function for the form that does a couple of things and then sends the results back to jQuery by doing:

    print json_encode($the_form_contents);

  • The ajax handler, which is expecting a json object, gets the results and sticks them into the body of the modal, and puts it up.

In moving this code to D7, I've been following the same route. It almost works, but the ajax call continually fails because the response string from the server includes the HTML from a page template, up to the point where the "exit" in the theming function kills the PHP request. That HTML is obviously not a expected part of the JSON object, but I can't figure out how to get rid of it. I've tried using drupal_json_encode in place of json_encode, and replacing the "print" statement with a call to drupal_json_output, but the page template stuff keeps coming. I'm definitely not adding it to what's produced in the theme function, so there must be some other part of Drupal that's squirting it out in ajax's direction.

Any ideas? This has REALLY been driving me crazy this afternoon. Thanks!

1 Answer 1


I don't know that this counts as a duplicate, but the answer by "jeroen" on https://stackoverflow.com/questions/247991/displaying-a-drupal-view-without-a-page-template-around-it got me past this. It's a greasy little hack (no offense, jeroen!), but it seems to work.

  • BTW, the finally-working version of this uses drupal_json_output($output) to encode the response for JSON. print json_encode($output) also seems to work, although there might be differences based on what kinds of things you're trying to send over.
    – Jim Miller
    Oct 18, 2012 at 15:37

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.