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
<?php// the following is a
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.
Can't get this to work :(...
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?
In D7 I believe you have to
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.
Thank you Jaypan... although
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.
Working D7
Working D7 example:
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.
Thank you
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
What happens when you try?
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.
you can also use this as
you can also use this as default value
$_REQUEST values are
$_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.