Hi,

I am pretty new in Drupal. Currently I try some AJAX stuff, which could be interesting in the future for me.

I have a form and theme a table. In one colum is a selectlist.
In the callback funktion, i wanna sava the option in a database.

My problem:
When I run first the AJAX, I can get the input from $form_state['input'].
Second time its not working. it might be that i return something wrong.

$el = array(
		'#id' =>  $c . '-el',
		'#type' => 'select',
		'#options' => $el_options,
		'#default_value' => $default_value,
		'#ajax' => array(
		  'callback' => 'ajax_save_option_callback',
		  'event'=>'change',
		 ),
	);

Callback:

function ajax_save_option_callback($form, $form_state){
	if(array_key_exists('table', $form_state['input'])){
		foreach($form_state['input']['table'] as $key => $value){
			$value = $value['value'];
			$id = $key;
			watchdog('LOG', 'ID: '.$id.' Value: ' .$value);
// db insert....
		}
	}
	return $form;
}

Watchdog shows me a lot of warning. (foreach, array parameter 2...) I am sure this comes from the wrong return.
In addition, whats right, $form or &$form?

Thanks a lot in advance!

Comments

nevets’s picture

What is the context of your code? Is this part of a form? You very rarely want to return the whole form.

This

$el = array(
        '#id' =>  $c . '-el',

implies you have more than one element using the callback but the code does not reflect this.

This

    if(array_key_exists('table', $form_state['input'])){

assumes $form_state['input'] exists, I would write the check as

if ( !empty($form_state['input']) && !empty($form_state['input']['table']) ) {

And I think you should be using $form_state['values'] instead of $form_state['input'].

ztiroM’s picture

thanks for the fast answer!
ya its a part of a form.
I do have more then one element($c is a counter). its a table and in each row, there is this selectlist.

with your checks, you check just the $form_state. But i think the problem is somewhere else:
i have my form in form.inc and the callback function in callback.inc.
After i run my ajax first time, i get the log in the watchdog including many errors from the form.inc file.
In the second run, everything is empty...

I have no clue where to start..

nevets’s picture

What are the errors?

How are you "including" form.inc and callback.inc?