开发者

Drupal onChange auto populate another CCK select list

I have built a multistep form using CCK, however I have a couple of issues outstanding but, not sure if CCK can help.

In one step of the form I have 2 select boxes, the first is auto populated from the vocabulary table with开发者_如何学JAVA the following code and all woks well.

$category_options = array();
$cat_res = db_query('select vid, name from vocabulary WHERE vid > 1 ORDER BY name ASC');
while ($cat_options = db_fetch_object($cat_res)) {
  $category_options[$cat_options->vid] = $cat_options->name;
}
return $category_options;

What I would like to do is, when a user selects one item from the vocablulary list it auto populates another select box with terms from the term_data table. I have 2 issues;

1) I have added the following code to the second select list, just to make sure it works (IT DOESN'T). There a multiple terms associated with each vocabulary, but the second sql statemant only returns one result when it should return several, (SO SOMETHING WRONG HERE). For example in the term_date table there are 6 terms with the vid of 3, but I only get one added to select list.

$term_options = array();  
$term_res = db_query('select vid, name from term_data WHERE vid = 3 ORDER BY name ASC'); while ($options = db_fetch_object($term_res)) {    
$term_options[$options->vid] = $options->name;  
}  
return $term_options;

2) Can I add an onChange to the first select list to call a function to auto populate second list using CCK, or do I have to lean towards doing my entire form using the FORM API.

Any help or thoughts would be very much appreciated.


It seems to be a mistake in the query that gets terms. I tried to correct:

$term_res = db_query('select tid, name from term_data WHERE vid = 3 ORDER BY name ASC'); 
while ($options = db_fetch_object($term_res)) {    
  $term_options[$options->tid] = $options->name;
}

In your code you selected vid that is actually equal for all terms. Then you added terms names to $term_options array under the same key => so you got only 1 element.

Considering the second question: I would send the whole data structure (all vocabularies and their terms) as json to the client (insert a js script to the page from your drupal code) and implement the desired functionality with jquery.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜