Community

After submit displaying results of a query on the same page

I have a form with fields like surname etc in it.
In my _validate function I search my db for clients with these details.
If none are found I display an error using form_set_error.
If clients are found I want to stay on the same page with the form fields populated as they were before submit was hit but also show the results of the search as a list below my search fields.

I have set $form['#redirect'] to FALSE and on submit my form field values remain as I want, but I have no idea of how to display my search results.

Can anyone help?

<?php
function client_search_form()
{   
   
$form['search_firstname'] = array(
   
'#type'  => 'textfield',
   
'#size' => 25,
   
'#attributes' => array('title' => t("Client's forename")),
  );
   
$form['search_surname'] = array(
   
'#type'  => 'textfield',
   
'#size' => 25,
   
'#attributes' => array('title' => t("Client's surname")),
  );
   
$form['search_dob'] = array(
   
'#type'  => 'textfield',
   
'#size' => 14,
   
'#attributes' => array('title' => t("Client's date of birth")),
  ); 
 
$townList = helper_get_locations();
   
$form['search_town'] = array(
   
'#type'  => 'select',
   
'#options' => $townList,
   
'#attributes' => array('title' => t("Client's town")),
  );
 
$form['client_search'] = array(
   
'#type'  => 'submit',
   
'#value' => t('Search'),
  );
 
$form['#redirect'] = FALSE;
 
$resetHelp = t('Click here to clear your current selections');
 
$form['client_clear'] = array(
       
'#value' => '<input class="form-button" type="reset" value="Clear" />',
       
'#attributes' => array('title' => $submitHelp),
      );

  return
$form;
}

function
client_search_form_validate($form, &$form_state)
{  
 
$searchDetails = new stdClass();
 
$searchDetails->firstname = $form_state['values']['search_firstname'];
 
$searchDetails->surname = $form_state['values']['search_surname'];
 
$searchDetails->dob = $form_state['values']['search_dob'];
 
$searchDetails->town = $form_state['values']['search_town'];
 
$clients = findClients($searchDetails);
  if (
$clients == false)
  {
   
form_set_error('', 'No clients found');     
  }
}


// returns list of clients or false if none found
// $searchDetails is an object with following attributes:
// firstname, surname, dob, town
function findClients($searchDetails)
{   
    static
$clients = null;
    if (
$clients == null)
    {
       
$clients = false;
       
$first = $searchDetails->firstname;    
       
$surname = $searchDetails->surname;   
       
$dob = $searchDetails->dob;   
       
$town = $searchDetails->town;
         
$sql = "SELECT md.nid, md.field_md_first_name_value as firstname, md.field_md_surname_value as surname, md.field_md_town_value as town, md.field_md_dob_value as dob
                FROM content_type_client_main_details md
                WHERE md.field_md_first_name_value like '%$first%'
                AND   md.field_md_surname_value like '%$surname%'
                AND   md.field_md_dob_value like '%$dob%'
                AND   md.field_md_town_value like '%$town%'
                ORDER BY md.field_md_surname_value;"
;
         
$results = db_query($sql);
          if (
$results)
          {
             
$clients = array();
              while (
$row = db_fetch_object($results))
              {
                 
$clients[] = $row;
              }
          }
    }
     
      return
$clients;
}
?>

Comments

You can store the data in

You can store the data in $form_state['storage']. If there is a data in this element, the form is rebuilt, rather than built from scratched. If you don't need to store any data, I think you can use $form_state['rebuild'] = TRUE in your submit function.

Interested in hiring us? jaypan.com

Thanks Jay!

Thanks Jay!

nobody click here