2011-05-22 56 views
9

如何參數傳遞給Ajax回調函數在Drupal 7的形式API參數傳遞給Ajax回調函數在Drupal 7的形式API

​​

例如,如果我需要指定表單元素,使動作用ajax我需要元素名稱傳遞給函數,使用戶的操作和結果返回到另一個元素形式

我需要傳遞aruguments這個回調函數「回調」 =>「ajax_function_name_callback」

功能ajax_function_name_callback($ args1,$ args2,... $ form,$ form_state){return ..}

2 - 以及如何通過表單?

感謝..

如果我不知道它是從什麼算子的研究genrated的$ input_name我需要什麼告訴ajax_'function_name_callback這個字段的名稱,使

$element[$input_name] = array(
     '#type' => 'textfield', 
     '#size' => '41', 
     '#ajax' => array(
////////////////////////////////////////////////////////////////////////////////// 
// here how i tell the ajax call back about this arguments informationvlike parents of this field ... etc 
///////////////////////////////////////////////////////////////////////////////// 
       'callback' => 'ajax_'function_name_callback', 
      'method' => 'replace', 
      'event' => 'blur', 
       'effect' => 'fade', 
       'progress' => array('type' => 'throbber', 'message' => ''), 
     ), 
    ); 


function ajax_'function_name_callback($arg_position,$arg_fieldName,$form,$form_state) 
{ 
$form[$arg_position][$arg_fieldName][#value] = anotherFunction($form[$arg_position][$arg_fieldName][#value]); 
return $form[$arg_position][$arg_fieldName]; 
} 

回答

4

通過形式。不需要自定義回調。

ajax回調可以訪問表單中的所有信息。例如,您可以添加一個隱藏類型(發送到瀏覽器,可以使用JavaScript進行更改)或值(僅用於內部,可以包含任何類型的數據(如對象且不能由用戶更改))的表單元素。

如果你能給出一個更詳細的例子你可以做什麼,我可以給你更詳細的解釋。

+0

我需要傳遞aruguments這個回調函數 '回調'=> 'ajax_function_name_callback' – user764851 2011-05-22 15:42:22

+0

功能ajax_function_name_callback($ args1,$ args2 ,... $ form,$ form_state) { return .. } – user764851 2011-05-22 15:42:47

+0

是的,有什麼論點,他們從哪裏來?正如我所說,你應該通過它們的形式結構。 – Berdir 2011-05-22 15:57:16

15

使用此$form_state['triggering_element']這將告訴你觸發元素的名稱,並給你所有的屬性。

+0

非常感謝$ form_state ['triggering_element'],我在http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax/7#comment-16054上發佈了一個示例 – 2011-07-07 13:14:46

2

沒有辦法在ajax回調函數中傳遞額外的參數。

有關更多詳細信息,請參見381行包含/ form.inc。

function ajax_form_callback() { 
    list($form, $form_state) = ajax_get_form(); 
    drupal_process_form($form['#form_id'], $form, $form_state); 

    // We need to return the part of the form (or some other content) that needs 
    // to be re-rendered so the browser can update the page with changed content. 
    // Since this is the generic menu callback used by many Ajax elements, it is 
    // up to the #ajax['callback'] function of the element (may or may not be a 
    // button) that triggered the Ajax request to determine what needs to be 
    // rendered. 
    if (!empty($form_state['triggering_element'])) { 
    $callback = $form_state['triggering_element']['#ajax']['callback']; 
    } 
    if (!empty($callback) && function_exists($callback)) { 
    return $callback($form, $form_state); // TA-DAM! 
    } 
} 
0

可以使用$ form_state並把它傳遞給回調函數,例如

定義表單功能這樣

function form_function_name($form, $form_state){ 

$element['field name'] = array(
    '#type' => 'textfield', 
    '#ajax' => array(
     'callback' => 'ajax_function_name_callback', 
     'method' => 'replace', 
    ), 
); 

$form_state['var'] = array('variable' => 'My_costome_variable'); 
return $element; 
} 

function ajax_function_name_callback($form, $form_state){ 

# use of $form_state['var']; 
return .. 
} 

注意,如果你想改變form_state [「變種」 ] in ajax_function_name_callback您必須使用

function ajax_function_name_callback($form, &$form_state) 

而不是

function ajax_function_name_callback($form, $form_state) 
1
The #ajax has an element called parameters, and can use as follows, 


$form['pro_div6_'.$id] = array(
          '#type' => 'button', 
          '#value' => t('order now'), 
          '#ajax' => array(
            'wrapper' => 'order_wrapper', 
           'callback' => 'product_order_callback', 
           'parameters'=>array('param1'=>'param1_vale','param2'=>'param2_vale','param3'=>'param3_vale'), 

           ), 

        ); 
And you can get this values from the call back function as, 
    function product_order_callback($form, $form_state){ 

echo "<pre>";print_r($form_state['triggering_element']); exit; 

    } 


will get an output like, 
![Please find the screen shot][1] 


    [1]: http://i.stack.imgur.com/75PoU.jpg 

享受編碼:)

1

我也有同樣的問題。我的解決方案是爲表單定義一個隱藏字段,然後獲取回調中的隱藏值。例如:

$form['departure_city_1']= array(
    '#type' => 'select', 
    '#options'=>$destination_array, 
    '#weight'=>1, 
    '#ajax' => array(
     'callback' => 'ajax_example_autocheckboxes_callback', 
     'wrapper' => 'checkboxes-div', 
     'method' => 'replace', 
     'effect' => 'fade', 
    ), 

); 

$form['skybird_token']= array(
    '#type' => 'hidden', 
    '#value' => $token, 
); 

然後在回調:

function ajax_example_autocheckboxes_callback($form, $form_state) { 
    $token=$form_state['values']['skybird_token']; 
} 
0

的可能方式的一些結論,傳遞變量到AJAX回調:

  1. 您的變量保存到$ form_state ['YOUR_VAR_NAME'],然後在回調中使用。
  2. 使用前面提出的隱藏類型創建表單元素。
  3. 保存您的變量爲某種形式的元素屬性,如:

    
    $element['field_name'] = array(
        '#type' => 'textfield', 
        '#ajax' => array(
        'callback' => 'ajax_function_name_callback', 
        'method' => 'replace', 
        'event' => 'blur', 
        'effect' => 'fade', 
        'progress' => array(
         'type' => 'throbber', 
         'message' => '', 
        ), 
    ), 
        '#attributes' => array(
        'data' => array('some_data'), 
        'id' => array('some_id'), 
    ), 
    ); 
    
    function ajax_function_name_callback($form,$form_state) { 
        $data = $form['field_name']['#attributes']['data'][0]; 
        $id = $form['field_name']['#attributes']['id'][0]; 
    }