Closed (fixed)
Project:
Examples for Developers
Version:
6.x-1.x-dev
Component:
AJAX Example
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
27 Aug 2010 at 08:33 UTC
Updated:
13 Nov 2012 at 02:23 UTC
Hi,
I used the almost exact same code as the example for dependant dropdown except that i linked the dropdown elements to a mysql database.
Problem is that by default evrything seems ok, first dropdown show all options and second shows dependent options based on my master_selection and when i try to select something else in the master one the dependent dropdown disapear, no error, just that .. Also even if i put my master_selection as a default value for the master one it won't do it .. even if an echo show that the var contain the right info .. hopes you can get it clear ...
here's my code:
function matches_admin($form_state) {
$form = array();
$query = "SELECT * FROM " . "{teams_entries}";
$query_result = db_query($query);
$query2 = "SELECT * FROM " . "{matches_tournaments}";
$query_result2 = db_query($query2);
$form['Add_form'] = array(
'#type' => 'fieldset',
'#title' => t('Add a new match'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['Add_form']['Opponement'] = array(
'#type' => 'fieldset',
'#title' => t('Opponement details'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$teams_names = array();
while ($matches = db_fetch_object($query_result)) {
$teams_names[$matches->tid] = $matches->name;
}
$master_selection = !empty($form_state['values']['home_team']) ? $form_state['values']['home_team'] : $teams_names[25];
$form['Add_form']['Opponement']['home_team'] = array(
'#type' => 'select',
'#title' => t('Home team'),
'#options' => $teams_names,
'#default_value' => $master_selection,
'#description' => t('Select the playing team in this match.'),
'#ahah' => array(
'path' => 'admin/esport/js',
'wrapper' => 'dependent-dropdown-wrapper',
// 'event' => 'change', // default value: does not need to be set explicitly.
),
);
$form['Add_form']['Opponement']['dependent_dropdown_holder'] = array(
'#tree' => TRUE,
'#prefix' => '<div id="dependent-dropdown-wrapper">',
'#suffix' => '</div>',
);
$form['Add_form']['Opponement']['dependent_dropdown_holder']['dependent_dropdown'] = array(
'#type' => 'select',
'#title' => t('Dependent Dropdown (changes based on master dropdown)'),
// when the form is rebuilt during processing (either AJAX or multistep),
// the $master_selction variable will now have the new value and so the
// options will change.
'#options' => matches_get_map_list($master_selection),
);
$form['Add_form']['Opponement']['matches_home_lineup'] = array(
'#type' => 'textfield',
'#title' => t('Home team lineup'),
'#default_value' => '',
'#size' => 30,
'#maxlength' => 255,
'#description' => t("Enter the home team lineup."),
'#required' => FALSE,
);
$form['Add_form']['Opponement']['matches_opponement'] = array(
'#type' => 'textfield',
'#title' => t('Opposite team'),
'#default_value' => '',
'#size' => 25,
'#maxlength' => 255,
'#description' => t("Enter the opposite team."),
'#required' => TRUE,
);
$form['Add_form']['Opponement']['matches_opponement_lineup'] = array(
'#type' => 'textfield',
'#title' => t('Opposite team lineup'),
'#default_value' => '',
'#size' => 30,
'#maxlength' => 255,
'#description' => t("Enter the opposite team lineup."),
'#required' => FALSE,
);
while ($tournaments = db_fetch_object($query_result2)) {
$tournaments_names[] = $tournaments->name;
}
$form['Add_form']['Opponement']['tournaments'] = array(
'#type' => 'select',
'#title' => t('Tournament'),
'#options' => $tournaments_names,
'#description' => t('Select the tournament.'),
);
$form['Add_form']['Opponement']['description'] = array(
'#type' => 'textarea',
'#title' => t('Match description'),
'#cols' => 60,
'#rows' => 5,
'#description' => t('Write details about the match.'),
);
$form['Add_form']['Map_1'] = array(
'#type' => 'fieldset',
'#title' => t('Map 1'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['Add_form']['Map_1']['matches_map_1_SCORE'] = array(
'#type' => 'textfield',
'#title' => t('Home score'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the home score for the first map."),
'#required' => FALSE,
);
$form['Add_form']['Map_1']['matches_map_1_SCORE_VISITOR'] = array(
'#type' => 'textfield',
'#title' => t('Visitor score'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the visitor score for the first map."),
'#required' => FALSE,
);
$form['Add_form']['Map_1']['matches__map_1_MAP_NAME'] = array(
'#type' => 'textfield',
'#title' => t('Map name'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the map name."),
'#required' => FALSE,
);
$form['Add_form']['Map_2'] = array(
'#type' => 'fieldset',
'#title' => t('Map 2'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['Add_form']['Map_2']['matches_map_2_SCORE'] = array(
'#type' => 'textfield',
'#title' => t('Home score'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the home score for the second map."),
'#required' => FALSE,
);
$form['Add_form']['Map_2']['matches_map_2_SCORE_VISITOR'] = array(
'#type' => 'textfield',
'#title' => t('Visitor score'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the visitor score for the second map."),
'#required' => FALSE,
);
$form['Add_form']['Map_2']['matches__map_2_MAP_NAME'] = array(
'#type' => 'textfield',
'#title' => t('Map name'),
'#default_value' => '',
'#size' => 15,
'#maxlength' => 255,
'#description' => t("Enter the map name."),
'#required' => FALSE,
);
$form['Add_form']['submit'] = array('#type' => 'submit', '#value' => t('Save'));
return $form;
}
function matches_get_map_list($key) {
$sql_team = "SELECT * FROM teams_entries WHERE name = '%s'";
$result_team = db_query($sql_team, $key);
while ($team_id = db_fetch_object($result_team)) {
$gid = $team_id->gid;
}
$sql = "SELECT * FROM matches_maps WHERE gid = '%d'";
$result = db_query($sql, $gid);
while ($maps = db_fetch_object($result)) {
$maps_names[$maps->mid] = $maps->name;
}
return $maps_names;
}
function matches_ahah_render() {
$form = matches_callback_helper();
$changed_elements = $form['dependent_dropdown_holder'];
// Prevent duplicate wrappers.
unset($changed_elements['#prefix'], $changed_elements['#suffix']);
$output = drupal_render($changed_elements);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
function matches_callback_helper() {
$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_build_id);
return $form;
}
///////////////////////////////////////
///////////////////////////////////////
/////RENDERING MATCHES ADMIN/////
///////////////////////////////////////
///////////////////////////////////////
function matches_admin_page() {
$test = '';
$head = array(
array('data' => t('ID'), 'field' => 'mid', 'sort' => 'asc', 'width' => '30'),
array('data' => t('Opponement'), 'width' => '30'),
array('data' => t('Home score')),
array('data' => t('Visitor score')),
array('data' => t('Opérations'))
);
$sql = "SELECT * FROM matches_entries" . tablesort_sql($head);
$result = db_query($sql);
while ($matches = db_fetch_object($result)) {
$score_home = $matches->map_1_score_home+$matches->map_2_score_home+$matches->map_3_score_home+$matches->map_4_score_home;
$score_visitor = $matches->map_1_score_visitor+$matches->map_2_score_visitor+$matches->map_3_score_visitor+$matches->map_4_score_visitor;
$rows[] = array(
array('data' => $matches->mid),
array('data' => $matches->team),
array('data' => $score_home),
array('data' => $score_visitor),
array('data' => ''.l('edit','admin/team/matches/edit/'. $matches->mid).' - '.l('delete','admin/team/matches/delete/'. $matches->mid).'')
);
}
$test .= theme_table($head, $rows);
$test .= drupal_get_form('matches_admin');
return $test;
}
function matches_admin_submit($form, &$form_state) {
db_query("INSERT INTO {matches_entries} (mid, team) VALUES ('','%s')", $form_state['values']['matches_opponement']);
drupal_set_message(t('Your form has been saved.') );
}
All help appreciated, thanks
Comments
Comment #1
rfayHi - Please start SIMPLE.
Start by just changing the existing dependent dropdown and implementing _ahah_example_get_first_dropdown_options() and _ahah_example_get_second_dropdown_options() using your database instead of the static initialization technique.
Note that the normal situation would be to have an associative array with key=>value instead of the Strings => Strings type thing that drupal_map_assoc() provides.
When you get those two functions implemented against your database, if they're working, the dependent dropdown should work. Then you can go forward from there actually implementing something for your own purposes.
-Randy
Comment #2
Craz commentedOk, i did what you said and indeed by trying slowly i made it work .. Now another question comes . How can i change multiple drop^down based on a single one ... i already tried a few things but nothing really worked .. any idea ?
if i try this :
i get a third not working dependent dropdown from nowhere even if my json response is :
Comment #3
Craz commentedEven tried this:
with no success ...
Comment #4
rfaySorry, I need you to explain more explicitly what you want to do.
Comment #5
sahasra commentedi need same multiple dropdown based on single product
i used ahah but not working
Comment #6
nicodv commentedI know this is for drupal6 (im orking with drupal7) but the case is I need to implement dependent dropdowns with retrieved data from a db, but i need these dependent dropdowns to be in the user registration form... any idea how to insert them in the registration form and feed the table 'users´with the choices of the user in these dropdowns?
thanks a lot (and sorry I couldn´t help you)
Comment #7
rfayYou use hook_form_alter() to do AJAX in a form that you didn't create. An example would be appreciated here. The most common request is an example of a hook_form_alter of the node page.
The basic dependent dropdown example should be adequate.
Comment #8
nicodv commentedI made it, I used #after_build and the dropdowns are in the form now. Still they don't validate and submit with the rest of the form. I guess is not the correct atribute.
Also (and more important, I dont know how to accomplish the "dependent query" Do you know where can i look for an example also?
Here is the code:
thanks a lot.
Comment #9
rfayPlease use http://drupal.stackexchange.com for support like this. Thanks!
Comment #10
mile23As always... Change status as needed.