ahah e campi di testo

1 contenuto / 0 new
ahah e campi di testo

Volevo scrivere in un campo di testo un tipo e se quel tipo esistava riempire i campi di testo sotto altrimenti lasciarli vuoti cosi che l'utente potesse completarli. Il problema è che se uso #value quando ricreo la form se il campo tipo non esiste anche se scrivo un valore nei tipi nelle altre lingue non mi inserisce al submit nulla. se uso default value invece non mi carica nulla.
Vi posto il codice che ho utilizzato nelle funzioni di haha.

<?php
//creazione della form
// creazione dei campi di testo
 
$form['prova']=array
   (  
'#type' => 'fieldset',
      
'#prefix'        => '<div id="tipoalimento">',
      
'#suffix'        => '</div>',   );
  
$form['prova']['tipo_it'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo  in italiano'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t('ES: primo'),
  
'#required'  => TRUE,
 
'#ahah' => array(
             
'path'    => 'riempi/js',   //sull'evento onchange viene chiamta questa pagina
             
'wrapper' => 'tipoalimento')    );
  
$form ['prova']['tipo_en'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo  in inglese'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#required'  => TRUE,
  );
  
$form ['prova']['tipo_es'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo in spagnolo'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#required'  => TRUE,   );
  
$form['prova']['tipo_sq'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo di alimento in albanese'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#required'  => TRUE,   );
  
//creazione del bottone submit
  
$form['submit'] = array(
  
'#type' => 'submit',
  
'#value' => t('invia'),
   );
return
$form;
}
?>

Funzioni ahah

<?php
function riempi_js() {
 
$tipo_it = $_POST['tipo_it'];
 
//drupal_set_message($id);
 
$query = "SELECT tipo_it,tipo_en,tipo_es,tipo_sq FROM tipoalimento where tipo_it like ('%s')";
 
$query = db_rewrite_sql($query);
 
$result=db_query($query,$tipo_it);
   if (!
$result)
      {
drupal_set_message("errore in inserisci alimento.inc");
      }
   
$res = db_fetch_array($result);
 
$form['prova']=array
   (  
'#type' => 'fieldset',
      
'#prefix'        => '<div id="tipoalimento">',
      
'#suffix'        => '</div>',   );
  
$form['prova']['tipo_it'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo  in italiano'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t('ES: primo'),
  
'#required'  => TRUE,
 
'#ahah' => array(
             
'path'    => 'riempi/js',
             
'wrapper' => 'tipoalimento')    );      
 
$form['prova']['tipo_en'] = array(
  
'#type' => 'textfield',
  
'#title' => t('tipo in inglese'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#value'=>$res[tipo_en],
   
//'#prefix'        => '<div id="tipoalimento">',
    //'#suffix'        => '</div>',
   
'#required'  => TRUE,
  );
    
$form ['prova']['tipo_es'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo in spagnolo'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#value'=>$res['tipo_es'],
  
'#required'  => TRUE,   );
     
$form['prova']['tipo_sq'] = array
   (
  
'#type' => 'textfield',
  
'#title' => t('Tipo albanese'),
  
'#size' => 25,
  
'#maxlengh' => 200,
  
'#description' => t(''),
  
'#value'=>$res['tipo_sq'],
  
'#required'  => TRUE,   );
 
$output = ahah_field_render($form, 'prova');
  print
drupal_to_js(array('data' => $output, 'status' => true));
  exit();
}
 
//copiata ma non ben capita (ma funziona !!!)
 
function ahah_field_render($fields, $name) {
 
// Cambia lo status del form
 
$form_state = array('submitted' => FALSE);
 
// Recupera l'id del form
 
$form_build_id = $_POST['form_build_id'];
 
// Recupera il form dalla cache
 
$form = form_get_cache($form_build_id, $form_state);
 
// Sostituisce il field
 
$form[$name] = $fields;
 
// Mette il form modificato in cache
 
form_set_cache($form_build_id, $form, $form_state);
 
$form += array(
   
'#post' => $_POST,
   
'#programmed' => false,
  );
 
// Rigenera il form
 
$form = form_builder($_POST['form_id'], $form, $form_state);
 
// Estrae il field modificato
 
$new_form = $form[$name];
 
// Renderizza e restituisce il field modificato
 
return drupal_render($new_form);
}
?>