2010-11-06 46 views
0

我讀過Drupal API表單文檔,但有些東西我只是沒有得到。

這可能很簡單,但我無法找到如何顯示錶單提交後提交的值。Drupal表單API - 提交時顯示錶單值

*編輯的代碼

<?php 

function createForm_enigmaFilters($form_state) { 
    $form = array(); 

    $form['#submit'][] = 'createForm_enigmaFilters_submit'; 

    $form['list'] = array(
     '#type' => 'markup', 
     '#prefix' => '<ul id="enigmaFilters">', 
     '#suffix' => '</ul>', 
    ); 

    $form['list']['startDate'] = array(
     '#id' => 'txtStartDate', 
     '#weight' => '0', 
     '#type' => 'textfield', 
     '#size' => '20', 
     '#title' => 'Date de début', 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    $form['list']['endDate'] = array(
     '#id' => 'txtEndDate', 
     '#weight' => '1', 
     '#type' => 'textfield', 
     '#size' => '20', 
     '#title' => 'Date de fin', 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    $form['list']['enigmaName'] = array(
     '#id' => 'txtEnigmaTitle', 
     '#weight' => '2', 
     '#type' => 'textfield', 
     '#size' => '100', 
     '#title' => 'Nom des énigmes', 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    $form['list']['lstAnswers'] = array(
     '#id' => 'lstAnswers', 
     '#weight' => '3', 
     '#default_value' => 'Uniquement les bonnes', 
     '#key_type' => 'associative', 
     '#type' => 'select', 
     '#options' => array(
      'Toutes les réponses' => 'Toutes les réponses', 
      'Uniquement les bonnes' => 'Uniquement les bonnes', 
     ), 
     '#multiple_toggle' => '1', 
     '#title' => 'Réponses', 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    $form['list']['promotionYear'] = array(
     '#id' => 'txtPromotionYear', 
     '#weight' => '4', 
     '#type' => 'textfield', 
     '#size' => '40', 
     '#title' => 'Année de promotion', 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    $form['list']['submit'] = array(
     '#id' => 'lnkApplyFilters', 
     '#weight' => '5', 
     '#type' => 'submit', 
     '#value' => t('Rechercher'), 
     '#prefix' => '<li>', 
     '#suffix' => '</li>', 
    ); 

    return $form; 
} 

function createForm_enigmaFilters_submit($form, &$form_state) { 
    echo $form_state['values']['startDate']; 
    echo $form_state['values']['endDate']; 
    echo $form_state['values']['enigmaName']; 
    echo $form_state['values']['lstAnswers']; 
    echo $form_state['values']['promotionYear']; 
} 

echo drupal_get_form('createForm_enigmaFilters'); 

?> 
+0

你的輸出是什麼?嘗試使用print_r($ myform)。 – hummingBird 2010-11-06 16:27:15

回答

1

據我所知,你不會看到這個輸出(雖然它會暫時存在),因爲表單會在調用提交處理程序後重定向到同一頁面 - 如果沒有,你會看到因爲您在提交處理程序中打印表單以及在第一個地方打印它的位置,因此會形成兩次表單!

我建議你失去了

echo drupal_get_form('createForm_enigmaFilters'); 

並更換您的其他呼叫呼應drupal_set_message,如:

drupal_set_message('start date: '.$form_state['values']['startDate']); 

這將意味着你的系統會顯示下一次頁面實際上是顯示 - 即在提交掛鉤被調用之後。

讓我知道,如果這是令人困惑/無法正常工作:)

+0

drupal_set_message很棒,謝謝!不過,我認爲我不能在我的情況下使用它。 我想使用表單的輸入作爲動態SQL查詢的過濾器。這樣,在提交回調函數中,我會將提交的值作爲參數調用我的自定義BuildAndDisplay(startdate,endate等)函數,並在重新加載時顯示查詢結果。想一想我該怎麼做?謝謝你的幫助,非常感謝! – 2010-11-06 18:56:43

+0

好的,drupal_set_message只是爲了顯示值,所以你可以看到他們被正確提交。爲了使頁面顯示結果,您通常需要重定向到另一個頁面,該頁面會顯示您的查詢和代碼以構建顯示。所以你可以把類似drupal_goto('mypage /'。form_state ['values'] ['startDate'] ...)。有一個谷歌。雖然聽起來好像意見可能是你前進的方向,但現在我知道更多。 – hookd 2010-11-06 19:09:29

+0

很酷我會試試這個。每個人都告訴我視圖應該是要走的路,而且我知道,但我非常初學,因此無法創建視圖來返回與我的複雜SQL查詢相同的視圖。但我猜這是另一個故事。再次感謝! – 2010-11-06 19:27:23

0

drupal_get_form應該叫createMyForm。

對於createMyForm_submit工作,你需要做的

$form['#submit'][] = 'createMyForm_submit'; 

在您的形式。這會向表單添加回調。

但是,您還應該遵守drupal編碼實踐,以最大限度地提高您的API效率。

+0

'createMyForm'電話是一個錯字,對此很抱歉。我使用提交回調更新了我的代碼,但提交後仍未顯示值。 – 2010-11-06 17:12:07

-1

我所做的只是使用ARG(x),其中x是任何序列中的文件夾編號爲。但這可能並不總是理想的。