Drupal 7 Query database and display results same page in custom module template I want to see the results on the same scree after submit.

But my results php error:


  Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32 bytes) in /var/www/html/includes/form.inc on line 368


I have tried to raise the memory limit in ini_set did not change.
I think that it is the cause the wrong my code.

My goal


Display the search results and input form on the same screen.
Search the external database with the value of the input form.
Keep the value of the input form.


Please help me with this.

Here is my code.

custom_search.module

function custom_search_menu() {
    $item['customsearch'] = array(
            'title'         =&gt; t('Form and Result'),
            'page callback' =&gt; 'custom_search_callback',
            'access arguments' =&gt; array('access content'),
            'type'          =&gt; MENU_CALLBACK,
    );
    return $item;
}

function custom_search_theme() {
    $path = drupal_get_path('module', 'custom_search');
    $rtn = array(
            'custom_search_template' =&gt; array(
                    'path' =&gt; $path,
                    'template' =&gt; 'node--customsearch',
            ),
    );
    return $rtn;
}

function custom_search_form($form, &amp;$form_state) {
    $form = array();

    $form['my_checkbox']=array(
            '#type'=&gt;'checkboxes',
            '#title'=&gt;t('checkbox'),
            '#options'=&gt;array(
                    '1'=&gt;t('option1'),
                    '2'=&gt;t('option2')
            ),
    );
    //There are some $ form[]

    $form['submit']=array(
            '#type'=&gt;'submit',
            '#value'=&gt;t('Submit')
    );

    return $form;
}

function custom_search_callback() {
    return theme('custom_search_template');
}

function custom_search_form_submit($form_id, &amp;$form_state) {
ini_set('memory_limit', '512M');
    $options = $form_state['input']['my_checkbox'];
    //external database query use input options
    //The external database has already connected and got data
    $results[] = array('1','2','3'); //sample results
    $results[] = array('2','3','4'); //sample results
    return theme('custom_search_template',array('results' =&gt; $results));
}


node--customsearch.tpl.php

&lt;div class=“row”&gt;
&lt;div class="col-md-4"&gt;
        &lt;?php $form = drupal_get_form('custom_search_form') ?&gt;
        &lt;form role="form" id='&lt;?php echo $form['#id'] ?&gt;' 
                            action="&lt;?php echo $form['#action'] ?&gt;" 
                            method="&lt;?php echo $form['#method'] ?&gt;"&gt;
          &lt;div class="row"&gt;
            &lt;?php echo drupal_render($form['my_checkbox']) ?&gt;
            &lt;?php echo drupal_render($form['form_id']);?&gt;
            &lt;?php echo drupal_render($form['form_build_id']);?&gt;
            &lt;?php echo drupal_render($form['form_token']);?&gt;
            &lt;?php echo drupal_render($form['submit']); ?&gt;
          &lt;/div&gt;
        &lt;/form&gt;
&lt;/div&gt;
&lt;div class=“col-md-8”&gt;
  &lt;h2&gt;results&lt;/h2&gt;
  &lt;table class=“table"&gt;
      &lt;!— I want results here —&gt;
      &lt;?php
      if(isset($results)) {
        foreach($results as $row) {
           echo "&lt;tr&gt;&lt;td&gt;";
           echo $row[0];
           echo "&lt;/td&gt;&lt;/tr&gt;";
        }
      }
      ?&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
I got it.

    $form_state['storage']['results'] = $results;
    $form_state["rebuild"] = TRUE;


(I do not know whether this method is correct.)

Modified code.

custom_search.module

&lt;?php
function custom_search_menu() {
    $item['customsearch'] = array(
            'title'         =&gt; t('Form and Result'),
            'page callback' =&gt; 'custom_search_callback',
            'access arguments' =&gt; array('access content'),
            'type'          =&gt; MENU_CALLBACK,
    );
    return $item;
}
function custom_search_theme() {
    $path = drupal_get_path('module', 'custom_search');
    $rtn = array(
            'custom_search_template' =&gt; array(
                    'path' =&gt; $path,
                    'template' =&gt; 'node--customsearch',
            ),
    );
    return $rtn;
}

function custom_search_form($form, &amp;$form_state) {
    $form = array();

    $form['my_checkbox']=array(
            '#type'=&gt;'checkboxes',
            '#title'=&gt;t('checkbox'),
            '#options'=&gt;array(
                    '1'=&gt;t('option1'),
                    '2'=&gt;t('option2')
            ),
    );

    $form['submit']=array(
            '#type'=&gt;'submit',
            '#value'=&gt;t('Submit')
    );
    if(isset($form_state['storage']['results']))
    {
            $form['results'] = array
            (
                    //'#value' =&gt; array('results' =&gt; $form_state['storage']['results']),
                    '#value' =&gt; $form_state['storage']['results'],
            );
    }

    return $form;
}

function custom_search_callback() {
    return theme('custom_search_template');
    //return theme('custom_search_template',array('results' =&gt; $results)); 
}

function custom_search_form_submit($form_id, &amp;$form_state) {
    $options = $form_state['input']['my_checkbox'];
    //external database query use input options
    $results[] = array('1','2','3'); //sample results
    $results[] = array('2','3','4'); //sample results
    $form_state['storage']['results'] = $results;
    $form_state["rebuild"] = TRUE;

}


node--customsearch.tpl.php

&lt;div class=“row”&gt;
&lt;div class="col-md-4"&gt;
        &lt;?php $form = drupal_get_form('custom_search_form') ?&gt;
        &lt;form role="form" id='&lt;?php echo $form['#id'] ?&gt;' 
                            action="&lt;?php echo $form['#action'] ?&gt;" 
                            method="&lt;?php echo $form['#method'] ?&gt;"&gt;
          &lt;div class="row"&gt;
            &lt;?php echo drupal_render($form['my_checkbox']) ?&gt;
            &lt;?php echo drupal_render($form['form_id']);?&gt;
            &lt;?php echo drupal_render($form['form_build_id']);?&gt;
            &lt;?php echo drupal_render($form['form_token']);?&gt;
            &lt;?php echo drupal_render($form['submit']); ?&gt;
          &lt;/div&gt;
        &lt;/form&gt;
&lt;?php
?&gt;
&lt;/div&gt;
&lt;div class=“col-md-8”&gt;
  &lt;h2&gt;results&lt;/h2&gt;
  &lt;table class=“table"&gt;
      &lt;!— I want results here —&gt;
      &lt;?php
      if(isset($form['results']['#value'])){
        $results = $form['results']['#value'];
        foreach($results as $row) {
           echo "&lt;tr&gt;&lt;td&gt;";
           echo $row[0];
           echo "&lt;/td&gt;&lt;/tr&gt;";
        }
      }
      ?&gt;
  &lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;