[RISOLTO]form 2 livelli con passaggio di parametri

4 contenuti / 0 new
Ultimo contenuto
[RISOLTO]form 2 livelli con passaggio di parametri

sto cercando di creare un form su 2 pagine, in modo da potermi passare il valore selezionato nel primo form nel secondo.

sto cercando di seguire questo esempio: http://drupal.org/node/717750
solo che, una volta che clicco su next, non ottengo la seconda pagina, bensi' il controllo passa alla funzione
content_copy_import_form2_submit($form, &$form_state) che gestisce il submit. qualcuno mi sa dire il perchè??

a parte quello che fanno le funzioni, non capisco come fare a passare dalla funzione
content_copy_import_form2 alla content_copy_import_next. perchè chiama subito submit??

aiuto!

<?php
  $items
['admin/content/types/ClassImport'] = array(
   
'title' => 'ClassImport',
   
'page callback' => 'content_copy_fanculo',
   
//'page arguments' => array('content_copy_import_form2'),
   
'access arguments' => array('access content'),
   
'type' => MENU_LOCAL_TASK,
   
'weight' => 5,
  );
  return
$items;
}
function
content_copy_fanculo(){
    return
drupal_get_form('content_copy_import_form2');
    }
function
content_copy_import_form2(&$form_state, $type_name = '') {
    if (isset(
$form_state['storage']['page_two'])) {
        return
content_copy_import_form_next();
      }
   
$selectFiles=_content_copy_files();
   
$form['#prefix'] = t('This form will import field definitions exported from another content type or another database.<br/>Note that fields cannot be duplicated within the same content type, so imported fields will be added only if they do not already exist in the selected type.');
   
$form['type_name'] = array(
   
'#type' => 'select',
   
'#options' =>  $selectFiles,
   
'#default_value' => $selectFiles[0],
   
'#title' => t('Content type'),
   
'#description' => t('Select the content type to import these fields into.<br/>Select &lt;Create&gt; to create a new content type to contain the fields.'),
  );
    
$form['next'] = array(
   
'#type' => 'submit',
   
'#value' => 'Next >>',
  );
  return
$form;
}
function
content_copy_import_form_next() {
  include_once(
'./'. drupal_get_path('module', 'content') .'/includes/content.admin.inc');
  include_once(
'./'. drupal_get_path('module', 'node') .'/content_types.inc');
 
$form['macro'] = array(
   
'#type' => 'textarea',
   
'#rows' => 1,
   
'#title' => t('Import data'),
   
'#required' => FALSE,
   
'#default_value' => ('pippo') ,
   
'#description' => t('Paste the text created by a content export into this field.'),
  );
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Import'),
  );
 
// Read in a file if there is one and set it as the default macro value.
 
if (isset($_REQUEST['macro_file']) && $file = file_get_contents($_REQUEST['macro_file'])) {
   
$form['macro']['#default_value'] = $file;
    if (isset(
$_REQUEST['type_name'])) {
     
$form['type_name']['#default_value'] = $_REQUEST['type_name'];
    }
   
$form['#prefix'] .= '<p class="error">'. t('A file has been pre-loaded for import.') .'</p>';
  }
    
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => 'Submit >>',
  );
  return
$form;
}
function
content_copy_import_form2_submit($form, &$form_state) {
    if (
$form_state['clicked_button']['#id'] == 'edit-next') {
   
$form_state['storage']['page_two'] = TRUE; // We set this to determine
                                               // which elements to display
                                               // when the page reloads.
    // Values below in the $form_state['storage'] array are saved
    // to carry forward to subsequent pages in the form.
   
$form_state['storage']['page_one_values'] = $form_state['values'];
  }
 
// Handle page 2 submissions
 
else {
   
/*
     Normally, some code would go here to alter the database with the data
     collected from the form. Sets a message with drupal_set_message()
     to validate working code.
     */
   
drupal_set_message('Your form has been submitted');
    unset (
$form_state['storage']); // This value must be unset for
                                    // redirection! This is because
                                    // $form_state['rebuild'] gets set to TRUE
                                    // when 'storage' is set. See code sample
                                    // #9 for more on this.
   
$form_state['redirect'] = 'node'; // Redirects the user.
}
   
$form_values = $form_state['values'];
   
$type_name = $form_values['type_name'];
   
$array_extimg=array('.xml');
   
$tipes=_content_copy_elencafiles("/var/www/provephp/",$array_extimg);
   
$file=$tipes[$type_name];
   
$type_label = node_get_types('name', $type_name);
   
$doc;//dichiarazione file xml, da tenere???
   
$toValue=content_copy_cClass("/var/www/provephp/".$file);
   
//echo $toValue;    works
    //inizio codice originale
   
$content = NULL;
   
// Convert the import formatted text back into a $content array.
    // Return if errors generated or not an array.
    // Use '@' to suppress errors about undefined constants in the macro.
   
@eval($form_values['macro']);
 
// Preliminary error trapping, must have valid arrays to work with.
 
....
}
 
?>

mi sono risposto da solo.

era l'else della funzione submit. come vedere era sbagliata una parentesi...

<?php
 
function content_copy_import_form2_submit($form, &$form_state) {
    if (
$form_state['clicked_button']['#id'] == 'edit-next') {
   
$form_state['storage']['page_two'] = TRUE; // We set this to determine
                                               // which elements to display
                                               // when the page reloads.
    // Values below in the $form_state['storage'] array are saved
    // to carry forward to subsequent pages in the form.
   
$form_state['storage']['page_one_values'] = $form_state['values'];
  }
 
// Handle page 2 submissions
 
else {bla bla bla
   
/*
?>

sono anche riuscito a capire come passare i parametri tra le due pagine. per farlo:
nella funzione in
<?
function content_copy_import_form2{
....
return content_copy_import_form_next($form_state)
}
?>
cambiare il valore in input della funzione di ritorno.
nella funzione content_copy_import_form_next fare una print_r(stampa tutto il form sotto forma di array) e cercare i parametri di interesse!

<?php
content_copy_import_form_next
($form)
print_r($form);
?>

spero che possa servire a qualcuno prima o poi. in alternativa è servito a me!

E io che credevo che le parolaccie forse 'reserved words' di PHP. Vengono usati spesso nelle tesi di laurea oggi giorno?

Più imparo, più dubito.

molto spesso credimi, soprattutto quando un modulo scritto 10 giorni fa torna a non funzionare. grrrrr