Somnath Pawar
Somnath Pawar

Reputation: 161

Problem when I am populating data into selectbox through ajax in Drupal

I have installed drupal 6, added some cck fields in one content type. Added two select box fields. I am taking the selected value of parent select box and as per that selection passing relates options to next select box field using Ajax. (e.g Country -> State. When user selects country I want to pass state values into next select box.)

But when I am submitting the form it gives the following error: "An illegal choice has been detected. Please contact the site administrator."

I don't know why it is not taking the ajaxified select box values while saving the node. Does somebody has the solution on it. Is there any solution to handle this dynamic select options in Drupal.

Thanks in advance.

Upvotes: 0

Views: 964

Answers (1)

manish nautiyal
manish nautiyal

Reputation: 2584

The same thing I'm working on drupal 7 and its work for me. Below is the code. Hope this help you. What I have done is on selection of car model car variant will change and the data save in the table.

function add_offer_form($form, $formstate) {

$form['add_offer_new_car_model'] = array(

    '#type' => 'select',
    '#required' => TRUE,
    '#options' => $car_model,
    '#ajax' => array(
        'effect' => 'fade',
        'progress' => array('type' => 'none'),
        'callback' => 'variant_callback',
        'wrapper' => 'replace_variant',
    ),
);

// Combo box to select new car variant

$form['add_offer_new_car_variant'] = array(

    '#type' => 'select',
    '#options' => array(),
    // The prefix/suffix provide the div that we're replacing, named by #ajax['wrapper'] above.
    '#prefix' => '<div id="replace_variant">',
    '#suffix' => '</div>',
);

// An AJAX request calls the form builder function for every change.

// We can change how we build the form based on $form_state.

if (!empty($formstate['values']['add_offer_new_car_model'])) {

    $model_id = $formstate['values']['add_offer_new_car_model'];
    $rows = array();
    $result = db_query("SELECT id, variant_name from {va_car_variant} where car_model_id in ($model_id,1) order by variant_name");
    while ($data = $result->fetchObject()) {
        $id = $data->id;
        $rows[$id] = $data->variant_name;
    }
    $form['add_offer_new_car_variant']['#options'] = $rows;
}

}

//////////////////////////////////////////////////////// ///////// FUNCTION FOR AJAX CALL BACK

function variant_callback($form, &$form_state) {

return $form['add_offer_new_car_variant'];

}

Upvotes: 1

Related Questions