Hi everyone ^^

I have a problem with the callback function of an AJAX link.
In few words: I have a table with some rows; each one has an AJAX link, “Details”, that change the content of the form below ("$form['corsi']['esito_corso']") depending on which link is clicked.

This is my code:

        //.........here I extract the information to put in the table
	
	$header = …;
	$rows = array();
	if (!empty($entries)) {
		foreach ($entries as $entry) {		
			$rows[] = array(
				'dettagli' => l('Details', 'examples/anag_dettaglio_callback/nojs/' . $corso .'/'. $entry_id, 
										array('attributes' => array('class' => array('use-ajax')))),
				// ….the other values
			);
		}
	}else {			  
		drupal_set_message(t('No entries meet the filter criteria.'));
	}
	
	$form['corsi']['table_corsi'] = array(
	    '#theme' => 'table',
	    '#header' => $header,
	    '#rows' => $rows,
	    '#empty' => t('Empty Rows'),
	    '#prefix' => '<div id="table_corsi_div"> <div class="vcase-anagrafiche-inline-field">',
	    '#suffix' => '</div></div>',
	);
	$form['corsi']['esito_corso'] = array(
	        '#type' => 'textfield',
		'#title' => t('Esito Corso'),
		'#size' => 15,
		'#default_value' => '--',
		'#attributes' => array('disabled' => 'disabled'),
		'#prefix' => '<div id="esito_corso_div"><div class="vcase-anagrafiche-inline-field">',
		'#suffix' => '</div>',
	);

I've added an item in the array of the hook_menu:

// A menu callback is required when using ajax outside of the Form API.
  $items['examples/anag_dettaglio_callback/%/%/%'] = array(
    'page callback' => 'anag_dettaglio_response',
    'access callback' => 'user_access',
    'page arguments' => array(2,3,4),
    //'access callback' => TRUE,
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

And the body of the function “ anag_dettaglio_response”:

function anag_dettaglio_response($type = 'ajax', $corso_call_id, $entry_id) {

  if ($type == 'ajax') {
   …....
   	$form['corsi']['esito_corso'] = array(
		'#type' => 'textfield',
		'#title' => t('Esito Corso'),
		'#size' => 15,
		'#value' => $dati[description''],
		'#attributes' => array('disabled' => 'disabled'),
		'#prefix' => '<div class="vcase-anagrafiche-inline-field">',
		'#suffix' => '</div>',
	);
….....

       $commands = array();
       $commands[] = ajax_command_replace("#esito_corso_div", render($form['corsi']['esito_corso']));
       $page = array('#type' => 'ajax', '#commands' => $commands);
       ajax_deliver($page);
 }else {
    …...
 }

The problem is that when I click a link for the first time, the form content change correctly, but from the second time on the content remains the same.

Sorry for my poor english, I hope that I've been clear about my problem.

Thanks everyone ^^

Comments

nevets’s picture

It is not clear what you are trying to do (ie what triggers the ajax code and what is suppose to change). I suspect the problem is related to how you are using ajax and that you have a form. Forms are only suppose to be changed in their form building function.

bandierabianca’s picture

Sorry XD
I'll try to explain myself better.
I have a table and a textfield form below. Each row of the table has a link that makes change the value of the textfield depending on the result of a query in the db.
Since i wanted to do that without reload the page, i have decided to use AJAX. Infact the declaration of each link (as field of a table row) is like this:

'dettagli' => l('Details', 'examples/anag_dettaglio_callback/nojs/' . $corso , array('attributes' => array('class' => array('use-ajax')))),

Under the definition of the table, i have defined the textfield, like this:

$form['corsi']['esito_corso'] = array(
        '#type' => 'textfield',
        '#title' => t('Esito Corso'),
        '#size' => 15,
        '#default_value' => '--',
        '#attributes' => array('disabled' => 'disabled'),
        '#prefix' => '<div id="esito_corso_div"><div class="vcase-anagrafiche-inline-field">',
        '#suffix' => '</div>',
    );

For example, if i click the link in the first row (Row A) it sends to this path

'examples/anag_dettaglio_callback/nojs/A'

The definition of this path is set in the hook_menu:

  $items['examples/anag_dettaglio_callback/%/%'] = array(
    'page callback' => 'anag_dettaglio_response',
    'access callback' => 'user_access',
    'page arguments' => array(2,3),
    //'access callback' => TRUE,
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

This recall the function "anag_dettaglio_response()"

function anag_dettaglio_response($type = 'ajax', $corso_call_id) {

  if ($type == 'ajax') {
   …....
       $dati['description'] = // Extraction of the value
       // this is the piece of the form that have to change
       $form['corsi']['esito_corso'] = array(
        '#type' => 'textfield',
        '#title' => t('Esito Corso'),
        '#size' => 15,
        '#value' => $dati['description'],
        '#attributes' => array('disabled' => 'disabled'),
        '#prefix' => '<div class="vcase-anagrafiche-inline-field">',
        '#suffix' => '</div>',
    );
….....
       // this command it's supposed to send the new version of the textfield form
       $commands = array();
       $commands[] = ajax_command_replace("#esito_corso_div", render($form['corsi']['esito_corso']));
       $page = array('#type' => 'ajax', '#commands' => $commands);
       ajax_deliver($page);
}

?>

The first time i click a link, the value of the textfield change correctly, but after this the value don't change anymore.
The query is correct becuase i text it with the ajax-command append.The problem with this command is that it "append" a new textfield form, like it is supposed to do. So when i click another link, it displays the right value, but in an other textfield under the first.

I hope that i've been clear this time ^^

Sorry again for my confusion and thank you very much!!!

jmlavarenne’s picture

Here is a useful piece of documentation on how to make ajax forms with Drupal: http://drupal.org/node/752056