Hi all,
I know this issue has been addressed here and there, but I've been searching for two hours and can't find a good explanation.

I want to use two values from a form to query a database, then display the results. I know this is probably doable with Views, but I'm building this module from scratch and would like to keep it simple - I'm using external data, and custom Views coding doesn't seem fun. Anyway, the form is fine, but once it gets submitted I can't figure out how to access the data in the following page to include the values in the query. I have been fiddling with 'page arguments' in the hook_menu of the second page, but I can't figure it out.

Here's my hook_menu...

function pts_menu() {

    $items = array();

    //Menu & URL settings for the module
  $items['admin/settings/pts'] = array(
    'title' => 'PTS module settings',
    'description' => 'Settings for the PTS module',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('pts_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

    $items['have_case'] = array(
    'title' => 'Do I have a Case?',
    'page callback' => 'have_case_all',
    'access arguments' => array('access pts content'),
    'type' => MENU_CALLBACK
  );

    $items['address_results'] = array(
     'title' => 'Address Search Details',
     'page callback' => 'address_results_all',
     'page arguments' => array('have_case_form'),
     'access arguments' => array('access pts content'),
     'type' => MENU_CALLBACK
    );

    $items['address_details'] = array(
     'title' => 'Address Details',
     'page callback' => 'address_details_all',
     'access arguments' => array('access pts content'),
     'type' => MENU_CALLBACK
    );

  return $items;
}

Here's my form...


function have_case_form($form_state) {

    $form = array(
        //'#action' => url($_POST['q'], array('query' => drupal_get_destination())),
        '#id' => 'have-case-form',
        '#submit' => array('have_case_form_submit'),
      );
      $form['house_number'] = array('#type' => 'textfield',
        '#title' => t('House Number'),
        '#maxlength' => 10,
        '#size' => 15,
        '#description' => 'Please enter only numbers, not your street name.',
        '#required' => TRUE,
      );
      $form['postal_code'] = array('#type' => 'textfield',
          '#title' => t('Postal/ZIP Code'),
          '#maxlength' => 5,
          '#description' => '5 digits only',
          '#required' => TRUE,
       );

     // $form['#multistep'] = TRUE;
     // $form['#redirect'] = FALSE;

      $form['submit'] = array('#type' => 'submit',
        '#value' => t('Submit'),
      );

     return $form;

}

function have_case_form_submit($form, &$form_state) {
    drupal_set_message(t('Form Submitted!'));
    $test = $form_state['values']['house_number'];

    drupal_set_message(t('Search for House #!'. $test));



    $form_state['redirect'] = array('address_results');
}

And here's the second page (the function I need to get the values into)...

function address_results_all($house_number, $postal_code) {
    
    //Change to PTS database
    db_set_active($name = 'pts');

  
    //Retrieve information for address with given data_id
    $result = db_query("SELECT * FROM {orps_data} WHERE orps_data_id=%d",$house_number);

    $r = db_fetch_array($result);

    $p .=$r['full_address'];


    //Reset to default database
    db_set_active($name = 'default');
    return $p;

   
}


Thanks for your help...

Comments

jaypan’s picture


// the following is a drupal_get_form() callback
function my_form($form_state)
{
  if(isset($form_state['storage']['value_a']))
  {
    $form['value_a'] = array
    (
      '#value' => '<p>' . t('The submitted value was: !a', array('!a' => $form_state['storage']['value_a'])) . '</p>';
    );
  }
  $form['a'] = array
  ( 
    '#type' => 'textfield',
    '#title' => t('Please enter a value'),
  );
  $form['submit'] = array
  (
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
}
function my_form_submit($form, &$form_state)
{
  $form_state['storage']['value_a'] = $form_state['values']['a']; // by setting $form_state['storage'], the form is rebuilt after submission.
}

There's an extremely basic example for you.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

loonyfoobar’s picture

Unfortunately I can't get this snippet to work in Drupal7...

isset($form_state['storage']['value_a']) is always returning "false" and I don't know why and what I am doing wrong?

Can someone please help me?

jaypan’s picture

In D7 I believe you have to set $form_state['rebuild'] = TRUE. Setting a value to storage does not rebuild the form like in D6.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

loonyfoobar’s picture

Thank you Jaypan... although a form rebuild is going to happen, still "isset" is returning 'FALSE'.
$form_state['storage']['value_a'] will not be set in my_form_submit and I don't know how...

It would be very nice if you could post a working example for D7 here.
Many thanks in advance.

jaypan’s picture

Working D7 example:

function jaypan_test_form($form, &$form_state)
{
	$form['text'] = array
	(
		'#type' => 'textfield',
		'#default_value' => isset($form_state['storage']['text']) ? $form_state['storage']['text'] : '',
	);
	$form['submit'] = array
	(
		'#type' => 'submit',
		'#value' => t('Submit'),
	);
	return $form;
}

function jaypan_test_form_submit(&$form, &$form_state)
{
	$form_state['storage']['text'] = $form_state['values']['text'];
	$form_state['rebuild'] = TRUE;
}

To be honest, I think this is essentially the same as D6, except that in D6 you don't need to set $form_state['rebuild'] to be TRUE, this happens automatically in D6 when saving values to $form_state['storage']. In D7 you need to explicitly state for the rebuild to happen in your submit function.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

pit1988’s picture

Hello Jaypan and thank you for your help you're awesome.

I have just question, can i use '#default_value' => isset($form_state['storage']['dropdown']) ? $form_state['storage']['dropdown'] : '',
on a $form['dropdown'] (type select ) in order to get values, that i'll use on a submit function to filter a table select.

Thank you

jaypan’s picture

What happens when you try?

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

waqarit’s picture

you can also use this as default value

'#default_value' => (isset($_REQUEST['your_field_name'])) ? $_REQUEST['your_field_name'] : '',
jaypan’s picture

$_REQUEST values are unsanitized, and actually the use of $_REQUEST itself can be a security risk. As such, I would advise not to use that.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.