Scenario: learning form API. Built myself a nice form with a list of aggregator items, with associated checkboxes. I click some checkboxes, hit submit. Now I want to : do some processing on the checked items (I think I got that right), and then rebuild a form with fields resulting from this processing.

All multiform docs I found were for FAPI 1.0 and 4.7.x. Not sure of how one would do that in 5.x.

Comments

robertdouglass’s picture

nicopepe’s picture

I made a module working with 4.7 and i can't make it running with dupal5 FAPI. I read the very interresting article from jeff, but i am not sure how to solve my problem.

My purpose is making a search form.
- Before displaying the result data i want to display first that form with searching value in that form, and the result data under it.
- I want a second button to make a new search which empty the form.

Here the result code i made, but it is not working. Please, any suggestion ?? Maybe a different structure ?

<?php
function exemple_menu($may_cache) {
  $items = array();

  if ($may_cache) {
      $items[] = array('path' => 'find',
                       'title' => t('find exemple'),
                       'callback' => 'exemple_show_results',
                       'access' => user_access('anonymous'),
                       'type' => MENU_CALLBACK); 
                       
      if(is_numeric(arg(1)) ) {                
            $items[] = array('path' => 'exemple/'.arg(1),
                         'title' => t('Exemples found in '.arg(1)),
                         'callback' => 'exemple_show_results',
                         'callback arguments' =>  str_pad(arg(1),5),
                         'access' => user_access('anonymous'),
                         'type' => MENU_CALLBACK);  
      }
  }  
}

function exemple_show_results() {

if(isset($_POST['raz'])){ // Used if raz button is pressed
    $raz = $_POST['raz'] ; 
}else{
    $raz = '' ; 
} 
    
if ((arg(0) == 'recherche') or $raz == 'New search') {
    $_SESSION['exemple_filter'] = NULL;    
    return drupal_get_form('exemple_search_form'); // Empty form for a new search
}else{
    $session = &$_SESSION['exemple_filter']; 
    $query = "SELECT ....."; // all records with the same CP or Town name with values from $session
}

$result = pager_query(db_rewrite_sql($query), 5,0,null,$valeurs);
 
$content = drupal_get_form('exemple_search_form'); // Display a form before result for a new search
$content .= "<br/>";
while ($exemple = db_fetch_object($result)) {
    $content .= $exemple->dbfield."<br/>";    
} //end while

if (!db_num_rows($result)) {
    $content .= '<H3>'.t('Nothing found').'<H3>';
}else{
    $content .= theme('pager', 0, 5, 0);
}

return $content;
} 

function exemple_search_form(){
  
  $session = &$_SESSION['exemple_filter'];
  $session = is_array($session) ? $session : array();
  
  $form['find'] = array('#type' => 'fieldset', '#title' => filt('Find exemple'), '#collapsible' => FALSE, '#collapsed' => FALSE, '#weight' => 1);

  $form['find']['CP'] = array(
         '#type' => 'textfield',
         '#title' => t('Postal Code'),
         '#default_value' => $session['CP'],
         '#size' => 5,
         '#maxlength' => 5
  );
  $form['find']['town'] = array(
        '#type' => 'textfield',
        '#title' => t('town'),
        '#default_value' =>  $session['town'],
        '#size' => 30,
        '#maxlength' => 100
  );
  // .... More fields
  
  $form['find']['submit'] = array(
      '#type' => 'submit',
      '#value' => filt('Find'),
      '#weight' => 98
    );     
    if(isset($_SESSION['exemple_filter']['session running'])){
         $form['find']['raz'] = array(
          '#type' => 'button',
          '#name' => 'raz',
          '#value' => t('New search'),  
          '#weight' => 99
        );                                             
    }
  return $form; 
}

function exemple_search_form_validate($form_id, $form_values){
    // Validate  CP  
  $_SESSION['mag_filter']['session running'] = TRUE;
  if ($form_values['CP'] == '' and $form_values['town'] == '') {
    form_set_error('', t('Please input CP or town name'));
  }
  if (strlen(trim($form_values['CP'])) <> 5 and strlen(trim($form_values['CP'])) > 1 ) {
    form_set_error('', filt('CP should be with 5 digits'));
  }
}

function exemple_search_form_submit($form_id, $edit) {

$_SESSION['exemple_filter'] = $edit;  
$_SESSION['mag_filter']['session running'] = TRUE;

return "exemple/".$edit['CP'];   // $edit['CP'] Used for the title only. For DB query we use $_SESSION['exemple_filter'] values.
}    
?>
nicopepe’s picture

Comment would be very welcome

FiReaNGeL’s picture

Saw that very interesting tutorial just after I posted this, and after an hour or two of banging my head against the wall (because I misunderstood timing of $form_values), it solved all my problems :) FAPI is awesome up so far, and not hard to understand at all.

---
Biology News Net