AnswerBun.com

Ajax callback does not replace the form but add a new field

Drupal Answers Asked by billyJoe on January 6, 2022

I use his code to populate a select after giving à zipcode.

$form['responsible']['zipcode'] = array(
      '#type'               => 'textfield',
      '#title'              => t('Zipcode'),
      '#default_value'      => empty($account->Dossier->responsable->adresse->codePostal) ? Null : $account->Dossier->responsable->adresse->codePostal,
      '#required'           => True,
      '#maxlength'          => 5,
      '#ajax' => array(
        'callback' => 'ajax_get_cities_callback',
        'wrapper' => 'edit-city',
        'method' => 'replace',
      ),
    );

    $form['responsible']['city'] = array(
      '#type'               => 'select',
      '#title'              => t('City'),
      '#options'            => array(),
      '#required'           => True,
    );

My callback

function ajax_get_cities_callback($form, $form_state){
  $client = new SoapClient('myWebservice',array("trace" => true,  "exceptions" => true,));
  $requestParams = array(
    'parameters' => array (
      'numSite' => 1058,
      'typeListes' => array(
        'RechercheTypeListe' => array(
          'typeListe' => 'LST_VILLE',
          'param1' => $form_state['values']['zipcode']
          )
        )
    )
  );
  $dataresponse->requestParams = $requestParams;
  $response = "";
  try {
      $response = $client->__Call('GetListes',$requestParams);
  }
  catch (Exception $e) {
    var_dump($e);
    return False;
  }

  $form['responsible']['city']['#options'] = array(
    0 => "aaaa",
    1 => "test2",
    2 => "test3"
  );

  return $form['responsible']['city'];

}

When I give a zipcode, a new select is created behind the first one.( picture here: https://www.uploady.com/#!/download/zjKvglzRTSe/ZIMYkjSAAaLrUFUq)
I tried to return only $form[‘responsible’][‘city’][‘#options’] but it does not fixe.

Any idea ?

Thank you

One Answer

You have to setup a wrapper on the field you want to update :

$form['responsible']['zipcode'] = array(
      '#type'               => 'textfield',
      '#title'              => t('Zipcode'),
      '#default_value'      => empty($account->Dossier->responsable->adresse->codePostal) 
                                    ? Null 
                                    : $account->Dossier->responsable->adresse->codePostal,
      '#required'           => True,
      '#maxlength'          => 5,
      '#ajax' => array(
        'callback' => 'ajax_get_cities_callback',
        'wrapper' => 'div_city_wrapper', // Changed here
        'method' => 'replace',
      ),
    );

    $form['responsible']['city'] = array(
      '#type'               => 'select',
      '#title'              => t('City'),
      '#options'            => array(),
      '#required'           => True,
      '#prefix'           => '<div id="div_city_wrapper">', // Added this
      '#suffix'           => '</div>', // Added this
    );

The callback function seems to be fine as it returns the wrapped form element ($form['responsible']['city'])

Answered by yilmi on January 6, 2022

Add your own answers!

Related Questions

Render fields url in tpl.php

2  Asked on October 26, 2021 by yoric

   

How to use Views Page + Attachment to show parent and children nodes?

1  Asked on October 26, 2021 by brian-huether

 

Hide new topic link based on the forum topics

1  Asked on October 26, 2021 by dominick

 

set min – max values drupal date_popup timepicker

2  Asked on October 26, 2021 by lorenzo-de-bie

   

Action links available options / caching

1  Asked on October 26, 2021 by marius-ilie

 

How to sort the query results?

1  Asked on October 26, 2021 by badm

   

Alter a path alias depending on a node property

1  Asked on October 26, 2021 by fizzz69

 

Webform variants as blocks

2  Asked on October 26, 2021 by lordaro

   

How do I redirect a domain but keep the URL structure?

1  Asked on October 26, 2021 by georgina-smith

     

How do I translate the weekday in a date?

1  Asked on October 26, 2021 by david-brugmans

     

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir