1

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

  1. Display the search results and input form on the same screen.
  2. Search the external database with the value of the input form.
  3. 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'         => t('Form and Result'),
            'page callback' => 'custom_search_callback',
            'access arguments' => array('access content'),
            'type'          => MENU_CALLBACK,
    );
    return $item;
}

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

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

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

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

    return $form;
}

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

function custom_search_form_submit($form_id, &$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' => $results));
}

node--customsearch.tpl.php

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

1 Answer 1

1

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

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

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

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

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

    return $form;
}

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

function custom_search_form_submit($form_id, &$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

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

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.