2

I am creating a custom module that I would like to use to display a form, from a module. The form queries a database and displays some information. The database connection is working, and the data has been retrieved successfully. The form has also been displayed successfully, but what I can figure out is how to link all together to have the page displayed again when submitting the form. My code is:

<?php

function menu($may_cache) {
    $items = array();
    $items['admin/reporting/report_details'] = array(
        'title' => 'Report: User details by stores',
        'access arguments' => array('access content'),
        'page callback' => 'say_report_details',
        'type' => MENU_CALLBACK,
    );  
return $items;
}

// This function should execute the logic if the $_GET variable is set
function say_report_details($values = array()) {
    // The $_GET logic will be somtehing like this
    // if (count($_GET) > 0) 
    // Get all the form values from the $_GET wit something like:
    // if (count($_GET) > 0) {
    // $start = mktime(0, 0, 0, $_GET['from_month'], $_GET['from_day'],

    $_GET['from_year']);
    //   $end = mktime(23, 59, 59, $_GET['to_month'], $_GET['to_day'], $_GET['to_year']);
    if ($_GET['store'] > 0) {
        $form = drupal_get_form("report_details_form");
        $output = theme("report_page", $form, $output);
        return $output;
    }


function report_details_form() {
    $form["search"] = array(
        '#type' => 'fieldset',
        '#title' => t('Search params'),
        '#collapsible' => FALSE,
        '#tree' => TRUE,
    );
    for ($i = 1; $i < 32; $i++) {
        $days_opt[$i] = $i;
    }
    for ($i = 1; $i < 13; $i++) {
        $month_opt[$i] = $i;
    }
    for ($i = 2008; $i < date("Y") + 3; $i++) {
        $year_opt[$i] = $i;
    }
    $form["search"]["from_day"] = array(
        '#type' => 'select',
        '#title' => 'Date from',
        '#options' => $days_opt,
        '#default_value' => (($_GET['from_day'] == "") ? date("d") : $_GET['from_day'])
    );
    $form["search"]["from_month"] = array(
        '#type' => 'select',
        '#title' => '&nbsp;',
        '#options' => $month_opt,
        '#default_value' => (($_GET['from_month'] == "") ? date("m") : $_GET['from_month'])
    );
    $form["search"]["from_year"] = array(
        '#type' => 'select',
        '#title' => '&nbsp;',
        '#options' => $year_opt,
        '#default_value' => (($_GET['from_year'] == "") ? date("Y") : $_GET['from_year'])
    );
    $form["search"]["to_day"] = array(
        '#type' => 'select',
        '#title' => 'Date to',
        '#options' => $days_opt,
        '#default_value' => (($_GET['to_day'] == "") ? date("d") : $_GET['to_day'])
    );
    $form["search"]["to_month"] = array(
        '#type' => 'select',
        '#title' => '&nbsp;',
        '#options' => $month_opt,
        '#default_value' => (($_GET['to_month'] == "") ? date("m") : $_GET['to_month'])
    );
    $form["search"]["to_year"] = array(
        '#type' => 'select',
        '#title' => '&nbsp;',
        '#options' => $year_opt,
        '#default_value' => (($_GET['to_year'] == "") ? date("Y") : $_GET['to_year'])
    );
    $result = db_query('SELECT taxonomy_term_data.name, taxonomy_term_data.tid FROM 
    taxonomy_term_data WHERE vid = 10');
    $strs = array("all" => "All");
    foreach ($result as $store) {
        $strs[$store->tid] = $store->name;
    }
    $form["search"]["store"] = array(
        '#type' => 'select',
        '#title' => 'Stores',
        '#options' => $strs,
        '#default_value' => $_GET['store']
    );
    $form["submit"] = array("#type" => "submit", "#value" => "Show report");
    return $form;
}
function theme_report_page($form, $result = array()) {
    $output = '
    <div id="report_form">
    '. $form .'
    </div> 
    <div id="report_result">
    '. $result .'
    </div>
    ';
    return $output;
}

function theme_report_details_form($form) {
    unset($form['search']['from_day']['#title']);
    unset($form['search']['from_month']['#title']);
    unset($form['search']['from_year']['#title']);
    unset($form['search']['to_day']['#title']);
    unset($form['search']['to_month']['#title']);
    unset($form['search']['to_year']['#title']);
    unset($form['search']['store']['#title']);
    $output = "<fieldset>
    <legend>Search params</legend>
    <div class='fieldtitles'>". t('Date From') .":</div>
    ". drupal_render($form['search']['from_day']) ."
    ". drupal_render($form['search']['from_month']) ."
    ". drupal_render($form['search']['from_year']) ."
    <div class='clearing'>&nbsp;</div>
    <div class='fieldtitles'>". t('Date To') .":</div>
    ". drupal_render($form['search']['to_day']) ."
    ". drupal_render($form['search']['to_month']) ."
    ". drupal_render($form['search']['to_year']) ."
    <div class='clearing'>&nbsp;</div>
    <div class='fieldtitles'>". t('Store') .":</div>
    ". drupal_render($form['search']['store']) ."
    <div class='clearing'>&nbsp;</div>
    ". drupal_render($form['submit']) ."
    </fieldset>";
    unset($form['search']);
    $output .= drupal_render($form);
    return $output;
}

function report_details_form_submit($form_id, $form_values) {
    $query = "";
    // This next line is the one we are having an issue right now, as is trowing an 
    error due to $form_values is empty for some UNKNOWN reason...
    foreach ($form_values['search'] as $key => $value) {
        $query .= "&". $key ."=". $value;
    }
    return array('admin/reporting/report_details', $query);
}
  • Please elaborate what you want to do...... – Dharmender Jun 8 '12 at 5:28
0

Try changing function report_details_form() to function report_details_form(&$form_state)

You will also need to change function report_details_form_submit($form_id, $form_values) to function report_details_form_submit($form_id, $form_state)

I would highly recommend reading through the Form API Quickstart Guide for this type of form.

Also, if you are not already using it, install the Devel module so you can access the dpm() function.

From here you can add dpm($form_values); to visually debug form variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.