简体   繁体   中英

custom drupal search module's form losing all post data when submitted

I am modifying an already contributed drupal module (Inline Ajax Search) to handle searching of a specific content type with some search filters (ie when searching for help documentation, you filter out your search results by selecting for which product and version of the product you want help with).

I have modified the module some what to handle all the search filters.

I also added in similar functionality from the standard core search module to handle the presenting of the search form and search results on the actual search page ( not the block form ).

The problem is that when i submit the form, i discovered that I'd lose all my post data on that submit because somewhere, and i don't know where, drupal is either redirecting me or something else is happening that is causing me to lose everything in the $_POST array.

here's the hook_menu() implementation:

<?php  
function inline_ajax_search_menu() {
    $items = array();
    $items['search/inline_ajax_search'] = array(  
        'title' => t('Learning Center Search'),
        'description' => t(''),
        'page callback' => 'inline_ajax_search_view',
        'access arguments' => array('search with inline_ajax_search'),
        'type' => MENU_LOCAL_TASK,
        'file' => 'inline_ajax_search.pages.inc',
    );
}
?>

the page callback is defined as such (very similar to the core search module's search_view function):

<?php  
function inline_ajax_search_view() {

    drupal_add_css(drupal_get_path('module', 'inline_ajax_search') . '/css/inline_ajax_search.css', 'module', 'all', FALSE );

    if (isset($_POST['form_id'])) {
        $keys = $_POST['keys'];

        // Only perform search if there is non-whitespace search term:
        $results = '';
        if(trim($keys)) {     
            require_once( drupal_get_path( 'module', 'inline_ajax_search' ) . '/includes/inline_ajax_search.inc' );

            // Collect the search results:
            $results = _inline_ajax_search($keys, inline_ajax_search_get_filters(), "page" );



            if ($results) { 
                $results = theme('box', t('Search results'), $results);
            }
            else {
                $results = theme('box', t('Your search yielded no results'), inline_ajax_search_help('inline_ajax_search#noresults', drupal_help_arg()));
            }
        }
        // Construct the search form.
        $output = drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
        $output .= $results;

        return $output;
    }
  return drupal_get_form('inline_ajax_search_search_form', inline_ajax_search_build_filters( variable_get( 'inline_ajax_search_filters', array() ) ) );
}
?>

from my understanding, things should work like this: A user goes to www.mysite.com/search/inline_ajax_search and drupal will process the path given in my url and provide me with a page that holds the themed form for my search module. When i submit the form, whose action is the same url (www.mysite.com/search/inline_ajax_search), then we go thru the same function calls, but we now have data in the $_POST array and one of them is indeed $_POST['form_id'] which is the name of the form "inline_ajax_search_search_form". so we should be able to enter into that if block and put out the search results.

but that's not what happens...somewhere from when i submit the form and get my results and theme it all up, i get redirected some how and lose all my post data.

if anybody can help me, it'd make me so happy lol.

drupal_get_form actually wipes out the $_POST array and so that's why I lose all my post data.

according to this: http://drupal.org/node/748830 $_POST should really be ignored when doing things in drupal. It's better to find a way around using it. One way is the way described in the link, making ur form data persist using the $_SESSION array. I'm sure there are various other and better ways to do this, but yeah, drupal_get_form was the culprit here...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM