Actually i want to implement ajax for state , country dropdowns.When user change the country , state list should be change in the state drop down.
The problem is that when user selects country in the drop down list the browser get hanged.
I have used dropdown example as a reference.
My code is here
user_menu() function part::
$items['user/user/callback'] = array(
'page callback' => 'get_states_callback',
'type' => MENU_CALLBACK,
'access arguments' => array('access content'),
);
Callback Function::
function get_states_callback()
{
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_id);
$changed_elements = $form['dropdown_state_wrapper'];
// unset($changed_elements['#prefix'], $changed_elements['#suffix']); // Prevent duplicate wrappers.
// db_query("UPDATE {test_table} set test='%s'",$form_state['#post']['first_name']);
drupal_json(array(
'status' => TRUE,
'data' => theme('status_messages') . drupal_render($changed_elements),
));
}
Form Function part ::
if (isset($form_state['my_values'])) {
$edit = $form_state['my_values'] + (array)$edit;
}
$countries = get_country_list();
$selected = isset($edit['country']) ? $edit['country'] : key($countries);
$form['account']['country'] = array(
'#type' => 'select',
'#title' => t('Country'),
'#default_value' =>$selected ,
'#options' => $countries,
'#required' => TRUE,
'#submit' => array('country_submit'),
'#ahah' => array(
'path' => 'user/user/callback',
'wrapper' => 'dropdown-state-wrapper',
// 'event' => 'change', // default value: does not need to be set explicitly.
),
);
$states = get_state_list($selected);
$form['account']['dropdown_state_wrapper'] = array(
'#tree' => TRUE,
'#prefix' => '',
'#suffix' => '',
);
$form['account']['dropdown_state_wrapper']['state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#default_value' =>$edit['state'] ,
'#options' => $states,
'#required' => TRUE,
);
Submit Handler Function::
function country_submit($form, &$form_state)
{
db_query("UPDATE {test_table} set test='%s'","SUBMIT HANDLER");
$values = $form_state['values'];
unset($form_state['submit_handlers']);
form_execute_handlers('submit', $form, $form_state);
$form_state['my_values'] = $values;
$form_state['rebuild'] = TRUE;
}
I believe these are all things that we need to implement basic ajax call.
I have debugged the flow and also tried other alternatives.
like when i commented the code which generates form then it is working correctly.It replaces the dropdown with the
hello
which i gave as a "data" argument to the drupal_json function.
But with code it is not working.Can you please figure it out where the problem is?
Comments
Comment #1
ipublic commentedsame here!
When the country is selected, the browser freezes