Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have created a form for creating some beaches. In that I have to give the value of continents,country ,name of beaches. Names of the countries and continent are stored in the taxonomy table. so if the user selects the continets, by using ajax it displays the list of countries belonging to the continent. my code look like..

function iks_beaches_create($node, &$form_state) {


drupal_set_breadcrumb(array(
    l('beaches', 'beaches'),
    'Add New Beaches'
));   
    $form['iks_beaches_continents'] = array(
    '#title' => 'Continents',
    '#description' => 'Taxonomy vocabulary defining Continents',
    '#type' => 'select',
    '#options' => iks_beaches_getTerms(variable_get(IKITESURF_BEACHES_CONTINENT, '')),
    '#required' => true,
    '#ajax' => array(
    'callback' => 'countryDisplay',
    'wrapper' => 'countryDisplay',
    'event' => 'change',
     ),
     );
    $form['countryDisplay']['iks_beaches_country']= array(
     '#type' => 'select',
     '#options' =>iks_beaches_getTerms(variable_get(IKITESURF_BEACHES_REGIONS, '')),
     '#title' => 'Country',
     '#required' => true,
     '#description' => 'Taxonomy vocabulary defining Country',
     );
     $form['regionDisplay']['iks_beaches_regions'] = array(
     '#title' => 'Regions',
     '#description' => 'Taxonomy vocabulary defining Regions',
     '#type' => 'select',
     '#options' => iks_beaches_getTerms(variable_get(IKITESURF_BEACHES_REGIONS, '')),
     '#required' => true,
     );

     $form['countryDisplay'] = array(
     '#type' => 'markup',
     '#prefix' => '<div id="countryDisplay">',
     '#suffix' => '</div>',
     );

the corresponding ajax function is

 function countryDisplay($form, &$form_state){
    $city = isset($form_state['values']['iks_beaches_continents']) ?        $form_state['values']['iks_beaches_continents'] : 'default';
    $query = new EntityFieldQuery();
    $entities = $query->entityCondition('entity_type', 'taxonomy_term')
                      ->propertyCondition('vid', variable_get('iks_beaches_countries'))
                      ->fieldCondition('field_continent', 'tid', $city)
                      ->execute();
    if(isset($entities['taxonomy_term']) && count($entities['taxonomy_term'])) {
    $countries = taxonomy_term_load_multiple(array_keys($entities['taxonomy_term']));       
    foreach($countries as $coutry){
   $options[$coutry->tid]=$coutry->name;
   }       
    }
    $form['countryDisplay']['iks_beaches_country'] = array(
         '#type' => 'select',
         '#options' => $options,
         '#title' => 'Country',
         '#required' => true,
         '#description' => 'Taxonomy vocabulary defining Country',        
        );
return  $form['countryDisplay'];
}

But countries value is not displaying on form submit

function iks_beaches_create_submit($form, &$form_state) {
echo "<pre>";print_r($form_state['values']);echo "</pre>";exit; }

Array
(
    [iks_beaches_continents] => 673
    [iks_beaches_regions] => 783
    [location_name] => hdnffgh 
    [description_location] => fgdh fgh fg hrt yry bnmbv mbcsd rty
    [travelling_details] => fh ty vcrsw t ytrywswe5
    [location_facilities] => ry  yuf d sdaf sa warewe svbvcber
    [tags] => e
    [longtitude] => 23.536343
    [latitude] => 45
    [images0] => 13803
    [images1] => 0
    [images2] => 0
    [images3] => 0
    [images4] => 0
    [images5] => 0
    [images6] => 0
    [images7] => 0
    [images8] => 0
    [images9] => 0
    [submit] => Publish Beach
    [form_build_id] => form-ZpuHlTPwSjJ2ElN_nPlSTsmHdQC6mmvSyKQOfMy4xvM
    [form_token] => n6vBuvZLJJNdcjdYERIsOm2REge8JoJMOSMmEuzpom8
    [form_id] => iks_beaches_create
    [op] => Publish Beach
)
share|improve this question

1 Answer

up vote 1 down vote accepted

Your mistake is a misconception about AJAX callback function. In Drupal, all permanent changes to a form needs to be done in form creation function, in your case iks_beaches_create. Function pointed in ['#ajax']['callback'] has the only purpose to determine what part of form is supposed to be updated. It can change $form, but these changes are one-time only, and preparing form to save values counts as a second time. Move all your form assembly logic to iks_beaches_create, so you could trim callback to

function countryDisplay($form, &$form_state){
  return  $form['countryDisplay'];
}

and it will work.

share|improve this answer
yes it works... – Ajmal Aug 21 at 11:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.