2012-06-08 50 views
0

我正在創建一個自定義模塊,我想用它來顯示錶單,從模塊中獲取。表單查詢數據庫並顯示一些信息。數據庫連接正在工作,並且數據已成功檢索。該表單也已成功顯示,但我可以弄清楚的是如何將所有鏈接在一起以在提交表單時再次顯示頁面。我的代碼是:如何在Drupal中提交表單調用自己

<?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); 
} 

回答

0

請閱讀&按照mikewink提供的答案。

另外在我創建了菜單項數組三種工作方式如下:

$items = array(); 
$items['admin/config/system/dataforms/SiteAccessform']=array(
    'title'=>'Site Access' //this should be watever the Title of your form is 
    ,'description'=>'Configuration for the Data Forms module' //shows up by your menu item as it's description (where applicable) 
    ,'page callback'=>'drupal_get_form' //this, as I understand it, is required for forms to work properly (& may be part of the cause of your problems) 
    ,'page arguments'=>array('dataforms_main_form') //IMPORTANT this is the actual name of your function that will be executed to produce the form 
    ,'access arguments'=>array('administer settings for dataforms') //?? 
    ,'type'=>MENU_CALLBACK//these work when the path is accessed though no menu items are created 
    , 
); 

數點,當你在與它的頁面參數的特殊「菜單」項數組元素設置爲您的表單,它認爲它只有一種形式。也就是說,這個函數將是唯一能夠創建表單的函數(以我的經驗)。因此,如果適用,使用該函數中的邏輯來產生不同的形式,(而不是嘗試擁有一個調用另一個函數的按鈕......這對我來說不起作用)。

另外,再次回顧一下mikewink的回答,$ form_state變量是存儲您要注意的狀態的方法,不是標準的$ _GET或$ _POST變量。

此鏈接可能是溫和的幫助:https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7

但是我收集來自實例模塊最有價值的信息:https://drupal.org/project/examples

祝您好運!