Hi everyone.
I'm an italian girl in drupal-trouble. XD

I want to write a module with a textfield, a submit button and a table.
The starting page displays an empty textfield, the button and the table that shows all the records.
When the user writes something in the textfield and clicks the button, the web page refreshes itself: then the table should contain only the records with the same Name Field as the string that the user wrote. Unfortunately, it doesn't works: the new page displays is the same as before (even if the query produced a correct table).

This is the code I use:

<?php
function prova_db_advanced_list($form, &$form_state) {

     
$form = array();

     
$form['textfield_input'] = array(
       
'#type' => 'fieldset',
       
'#title' => t("Compilare i campi in base alla ricerca che si desidera effettuare.")
      );

     
$form['textfield_input']['nome'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Inserisci un nome'),
       
'#size' => 15,
      );
     
$form['textfield_input']['submit'] = array(
       
'#type' => 'submit',
       
'#value' => t("Find"),
      );

     
$select = db_select('persone_prova_db', 'p');
     
     
// Join the users table, so we can get the entry creator's username.
     
$select->join('indirizzi_prova_db', 'i', 'p.residenza = i.id');
     
     
// Select these specific fields for the output.
     
$select->addField('p', 'id');
     
$select->addField('p', 'cognome');
     
$select->addField('p', 'nome');
     
$select->addField('p', 'codice_fiscale');
     
$select->addField('i', 'id');
     
$select->addField('i', 'via_piazza');
     
$select->addField('i', 'civico');
     
$select->addField('i', 'comune');
     
$select->addField('i', 'provincia');

     
// Make sure we only get items 0-49, for scalability reasons.
     
$select->range(0, 50);

     
// Get all entries in the prova_db table.
     
$entries = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
     
      if (!empty(
$entries)) {
       
$rows = array();
        foreach (
$entries as $entry) {
           
         
// Sanitize the data before handing it off to the theme layer.
         
$rows[] = array_map('check_plain', $entry);
        }
       
       
// Make a table for them.
       
$header = array(t('Id Persona'), t('Cognome'), t('Nome'), t('Codice Fiscale'),
                   
t('Id Indirizzo'), t('Via/Piazza'), t('Num. Civico'), t('Comune'), t('Provincia'));
      }else {     
       
drupal_set_message(t('No entries meet the filter criteria.'));
      }
     
     
$form['table'] = array(
     
'#theme' => 'table',
     
'#header' => $header,
     
'#rows' => $rows,
     
'#empty' => t('Empty Rows')
      );     
  return
$form;
}

function
prova_db_advanced_list_submit($form, &$form_state) {
     
drupal_set_message(t("Tabella"));
     
drupal_set_message(t("Il nome inserito è @name_to_find",  array('@name_to_find'=>$form_state['values']['nome'])));
     
$name_to_find = $form_state['values']['nome'];
     
$select = db_select('persone_prova_db', 'p');
     
     
// Join the users table, so we can get the entry creator's username.
     
$select->join('indirizzi_prova_db', 'i', 'p.residenza = i.id');
     
     
// Select these specific fields for the output.
     
$select->addField('p', 'id');
     
$select->addField('p', 'cognome');
     
$select->addField('p', 'nome');
     
$select->addField('p', 'codice_fiscale');
     
$select->addField('i', 'id');
     
$select->addField('i', 'via_piazza');
     
$select->addField('i', 'civico');
     
$select->addField('i', 'comune');
     
$select->addField('i', 'provincia');
     
$select->condition('p.nome', $name_to_find);

     
// Make sure we only get items 0-49, for scalability reasons.
     
$select->range(0, 50);

     
// Get all entries in the prova_db table.
     
$entries = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
     
      if (!empty(
$entries)) {
       
$rows = array();
        foreach (
$entries as $entry) {   
         
// Sanitize the data before handing it off to the theme layer.
         
$rows[] = array_map('check_plain', $entry);
        }
       
drupal_set_message(t("Table: @rows", array('@rows' => $rows)));
       
$form['table']= array('#type' => 'value', '#rows' => $rows);
         
$form_state['rebuild'] = TRUE;
      }else {     
       
drupal_set_message(t('No entries meet the filter criteria'));
      } 
}
?>

I don't understand where i'm wrong. I searched if someone else have the same trouble, but i haven't found the way to solve this problem.

Can someone help me???

Thanks ^^

Comments

nevets’s picture

You can simplify the problem by using a content type, user object or entity plus the views module and an exposed filter.

As for doing it with code, look at the search module for an example

bandierabianca’s picture

Thank you very much.
I'll take a look at that module and wish it could help me.

Jaypan’s picture

The Drupal form process works like this (note that this is shortened, and not complete):

1) Form is generated
2) User submits form
3) Form submit functions are run
4) The form is rebuilt fresh and shown to the user again (#1)

The problem for you is that you want to take the data submitted in step 2, and show it in step 1. To do this, you need to do three things:

1) Create a submit function
2) In the submit function, save the relevant data to $form_state['storage']
3) In the submit function, set $form_state['rebuild'] to TRUE.

By doing this, in step 4 above, the form will rebuild, but you will have access to the submitted data in $form_state['storage'], and can use this to build your table.

Here's an example I gave someone the other day: http://drupal.org/node/870120#comment-6413664

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

bandierabianca’s picture

Thank you so much. Your answer is excellent and very clear.
I'try immediatly using that procedure. I wish it works.
Bye ^^

bandierabianca’s picture

Hi. It works exactly like you said.
Here the solution:

<?php
function prova_db_advanced_list($form, &$form_state) {

     
$form = array();
     
$form['textfield_input'] = array(
       
'#type' => 'fieldset',
       
'#title' => t("Compilare i campi in base alla ricerca che si desidera effettuare.")
      );

     
$form['nome'] = array(
       
'#type' => 'textfield',
       
'#title' => t('Inserisci un nome'),
       
'#size' => 15,
       
'#default_value' => isset($form_state['storage']['nome']) ?
                                           
$form_state['storage']['nome'] : '',
      );
     
$form['submit'] = array(
       
'#type' => 'submit',
       
'#value' => t("Find"), 
      );

     
// Building the table 
     
$select = db_select('persone_prova_db', 'p');
     
     
// Join the users table, so we can get the entry creator's username.
     
$select->join('indirizzi_prova_db', 'i', 'p.residenza = i.id');
     
     
// Select these specific fields for the output.
     
$select->addField('p', 'id');
     
$select->addField('p', 'cognome');
     
$select->addField('p', 'nome');
     
$select->addField('p', 'codice_fiscale');
     
$select->addField('i', 'id');
     
$select->addField('i', 'via_piazza');
     
$select->addField('i', 'civico');
     
$select->addField('i', 'comune');
     
$select->addField('i', 'provincia');
     
      if (isset(
$form_state['storage']['nome'])){       
       
$select->condition('p.nome', $form[nome]); 
      }   

     
// Make sure we only get items 0-49, for scalability reasons.
     
$select->range(0, 50);

     
// Get all entries in the prova_db table.
     
$entries = $select->execute()->fetchAll(PDO::FETCH_ASSOC);
     
     
//$output = "<table id='restripe_table' style='border: 1px solid black'>";
     
$rows = array();
      if (!empty(
$entries)) {
        foreach (
$entries as $entry) {
           
         
// Sanitize the data before handing it off to the theme layer.
         
$rows[] = array_map('check_plain', $entry);
        }
      }else {             
       
drupal_set_message(t('No entries meet the filter criteria.'));
      }
     
// Make a table for them.
     
$header = array(t('Id Persona'), t('Cognome'), t('Nome'), t('Codice Fiscale'),
                   
t('Id Indirizzo'), t('Via/Piazza'), t('Num. Civico'), t('Comune'), t('Provincia'));
                   
     
$form['table'] = array(
     
'#theme' => 'table',
     
'#header' => $header,
     
'#rows' => $rows,
     
'#empty' => t('Empty Rows')
      );     

  return
$form;
}


function
prova_db_advanced_list_submit($form, &$form_state) {
   
$form_state['storage']['nome'] = $form_state['values']['nome'];
   
$form_state['rebuild'] = TRUE
}
?>

Now i try to make the research more complicated. I want the query return also the record containing the string the user inserted.
Here's the code:

<?php
        $select
->condition('p.nome''%' .db_like($form_state['values']['nome']). '%', 'LIKE');
?>

Thanks everyone.
Bye ^^

pit1988’s picture

really helpful thnx