need help on getting $form_state['values'] of forms added via ajax after submitted.

my codes as below,however it will not return any values of 'field_name' after submitted.Thanks in advance!

<?php
/**
 * hook_menu()
 */

function addstudent_menu(){
   
$items= array();
   
$items['user/%/add-students'] = array(
       
'title'=>t('添加學生賬戶'),
       
'page callback' =>'drupal_get_form',
       
'page arguments' => array(addstudent_form),
       
'access arguments' => array('create new users'),
       
'type' => MENU_LOCAL_TASK,


    );

    return
$items;

}


function
addstudent_form($form,&$form_state){
   
$form['#cache'] = TRUE;
   
$dropdown_source = taxonomy_get_tree(3);
    if(!empty(
$dropdown_source)){
       
$options = array('0'=> '--none--');

        foreach(
$dropdown_source as $item){ //we get all class term here
           
$key = $item->tid;
           
$value =$item->name;
           
$options[$key] = $value;
        }
    }else {
       
drupal_set_message(t('你还没有创建任何班级'),'error');
    }


   
$form['addstudent_wrapper'] = array(
       
'#title' => t('添加學生賬戶'),
       
'#type' =>'fieldset',
       
'#prefix'=>'<div id="fields_wrapper">',
       
'#suffix'=> '</div>',

    );


   
$form['addstudent_wrapper']['fields_head']['select_class'] = array(
       
'#title'=> t('班级'),
       
'#type' => 'select',
       
'#options'=>$options
   
);

   
$form['addstudent_wrapper']['fields_head']['student_qty'] = array(
       
'#title' => t('人數'),
       
'#type'=> 'textfield',
       
'#size'=> 10,

    );




   
$form['addstudent_wrapper']['fields_head']['add'] = array(
       
'#type' => 'submit',
       
'#value'=> '確認',
       
'#ajax' => array(
           
'callback' => 'addstudent_get_callback',
           
'wrapper' =>'fields_wrapper',

        ),
    );






   
$form['addstudent_wrapper']['fields_wrapper']=array(
       
'#tree' => TRUE,
    );





    return
$form;

}




function
addstudent_get_callback($form,&$form_state){
   
$form_state['rebulid'] = TRUE;
   
$i = $form_state['values']['student_qty'];
    return
$form['addstudent_wrapper']['fields_wrapper'] = _addstudent_fields($i);

}




function 
_addstudent_fields($i){
    for(
$a=0;$a<$i;$a++){

       
$form[$a]['field_number'] = array(
           
'#title' => '學號',
           
'#markup'=>$a+1,
           
'#tree' =>TRUE,
           
'#type'=>'markup'

       
);

       
$form[$a]['field_name'] = array(
           
'#title' => '姓名',
           
'#type'=> 'textfield',
           
'#tree' => TRUE,
           
'#parents' => array('field_name', $i, 'field_name'),
           
'#size'=> 10,
           
'#default_value'=>''
       
);
    }

   
$form['addstudent_wrapper']['submit'] = array(
       
'#type' => 'submit',
       
'#value' => 'Confirm',
       
'#submit' => array('addstudent_form_submit'),
    );


    return
$form;
}

function
addstudent_form_submit($form,&$form_state){

   
$values= $form_state['values']['field_name'];
   
$a = array_values($values);
    foreach(
$a AS $b){
        foreach(
$b as $c){
           
$r[] = $c;
        }
    }

   
dsm($values);

}
?>

Comments

Jaypan’s picture

I didn't really look through your code, but maybe this tutorial I wrote the other day can help: http://www.jaypan.com/tutorial/drupal-7-ajax-driven-multi-step-forms-non...

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

nevets’s picture

The form function is responsible for adding all form elements, the ajax callback is responsible for return the changed elements. The developers examples module includes several form ajax examples.