I have written a form that accepts user's input and based upon the input queries an external site and extracts the html of the result page from the external site. Is there a way to print html inside the form submit so that the results would appear on my drupal site. Following is my form_submit. (Drupal 7 is the version that I am working with)

   function tripal_search_form_submit($form, &$form_state) {
    switch ($form_state['triggering_element']['#name']) {
     case 'submit':
     $some_var = file_get_contents("http://legumeinfo.org/gb2/gbrowse/$organism/?q=$string");
     print $some_var //This is were I need help
     case 'reset':
     $form['items']['name'] = array(
     '#type' => 'markup',
     '#value' => '<input class="form-button" value="Reset" type="reset">',
     '#weight' => 2000,

3 Answers 3


You might try using the drupal api function drupal_set_message()


so in your case:

drupal_set_message($some_var); // will display the rendered html as a status message
  • 2
    But from what I understand to use file_get_contents() to render an external website content, without converting it to something useful and fitting, directly into your context makes no sense ... or I maybe misunderstand his question. Also using Drupals system message system only works with message regions enabled and will be in style of notice/warning/error messages. I am not sure if it is what he wants, otherwise he has asked for such alerts ...
    – nilsun
    Commented Nov 14, 2014 at 0:48
  • Well the results would "appear on his website". Commented Nov 14, 2014 at 3:26
  • Perhaps what to do then is to set a variable using variable_set(). Then when the form reloads after the submit handler, introduce a #markup element that displays the html fetched from the remote resource. The code for the form handler could use variable_get() to fetch the url, and display the html on the form after submission. This is assuming that the form redirects back to itself and not another url Commented Nov 14, 2014 at 3:29
  • @hrishi: could you post the code for your form handler, or provide additional details for what you are trying to accomplish? Commented Nov 14, 2014 at 3:36

I am not sure if I understood the question correctly, but if you don't mind to look at other solutions already there for that part about grabbing from external sources, I can only suggest to look at modules like: Prepopulate , Entityreference prepopulate, Quickpost bookmarklet, Simple bookmarklet, Bookmarklet and how they have achieved it. For sure, depending on your needs, it mayd not be exactly what you look for. But maybe a starting point or side by side inspiration ...

There are also maybe other useful questions around near to yours: Which hook do I need to use to pull external content in pages?

I also would recommend to give more details about your Drupal version etc. (tags) to gain more answers here. Hope it helps. Best regards.

  • Thanks for the reply, but the only thing I want to do is print $some_var, after the case: 'submit' executes
    – Hrishi
    Commented Nov 13, 2014 at 18:34
  • 3
    No problem. But I don't really see then the further question here since that what you ask for in the comment is what you have posted already in the code. It is not clear if you don't know how to print values of a $var in general or if you don't know how to get the content of the external source. If second the case, my first answer is still correct since it needs more than most people would expect to get this achieved. You should clearify your question and title and add a Drupal version tag, since some users only concentrate (filter) their attention and sorting tags on certain Drupal versions.
    – diqidoq
    Commented Nov 13, 2014 at 18:50

The drupal_get_form functionpProcesses a form array and produces the HTML output of a form. If there is input in the $_POST['edit'] variable, this function will attempt to validate it, using drupal_validate_form, and then submit the form using drupal_submit_form.

When you are building your form you want to set re-direct to false:

$form['#redirect'] = FALSE;

Please take a deep look at redirect here: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#redirect

Drupal helps you create and process forms right inside your module. The APIs Drupal offers make it very simple to make an addition or modification of your form as the forms are arrays and even the validation and submission happens in a different function. This modular approach keeps the form code in your module clean and easy to maintain. You also do not need to bother with the HTML details of the form if you use the Drupal form API – all the HTML is auto-generated. Have fun creating your next form in your Drupal module!

Take a look at this group post: [Modifying individual form elements html markup with form api]. I hope it can help you somehow.

Thank you.

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.