I need a little help here. In a custom module, I add a little form where you type your zip code, press a button and a drop down list populates (via ajax) with the stores that are close to that zip code so you can choose one of them. The problem is when you click on that button the callback is made but the zip code field remains empty, seems like I missing something that can bring the zipcode value to that part when the refreshes. some ideas?? thank you

    function mymodule_form($form, &$form_state) {

       $form['zip_code'] = array(
        '#type' => 'textfield', 
        '#title' => 'zip code',
        '#size' => 5,
        '#maxlength' => 5,
        '#required' => TRUE

      $form['button'] = array(
        '#type' => 'button', 
        '#value' => 'find stores',
        "#limit_validation_errors" => array(),
        "#name" => 'mybutton',
        '#ajax' => array(
          'callback' => 'reload',
          'wrapper' => 'stores-div',
          'method' => 'replace',
          'effect' => 'fade'
      $form['select_str'] = array(
        '#prefix' => '<div id="stores-div">',
        '#suffix' => '</div>'
      if(isset($form_state['values']['zip_code'])) {
        $arr = find_store($form_state['values']['zip_code']); //here is my main problem, $form_state['values']['zip_code']is always empty even after the callback 
      } else {
        $arr[99999] = 'theres none';
      $form['select_str']['stores'] = array(
        '#type' => 'select', 
        '#title' => 'stores near you:',
        '#options' => $arr,
        '#required' => TRUE
      $form['submit_button'] = array(
        '#type' => 'submit',
        '#value' => t('Send!'),
      return $form;
    function find_store($zc) {
        $fields = array(
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,"http://somepage.php");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
        $result=curl_exec ($ch);
        $arr = array();
        if($result!='none') {
            foreach($explode as $value) {
                $stores = explode('|',$value);
                $arr[$stores[0]] = $stores[1];


    } else {
        $arr[99999] ='theres none';
    return $arr;

function reload($form, &$form_state) {
  return $form['select_str'];

1 Answer 1


The values will be passed to the callback function reload instead of the original form function, therefore you should rebuild the form in your callback function - reload and pass back the element:

function reload($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
  $form = drupal_rebuild_form('mymodule_form', $form_state, $form);
  return $form['select_str'];
  • thank you, the select field is refreshing good now but when I choose one option and click the submit button, the form validator throws me this error "stores near you: field is required" no matter what option I choose
    – Alex
    Commented Mar 15, 2012 at 17:33
  • @Alex Add #limit_validation_errors => array() as a property of $form['button'], that'll stop the validation from running just for the AJAX request
    – Clive
    Commented Mar 15, 2012 at 17:34
  • @Clive $form['button'] already has that property, the problem is when I'm trying to actually submit the complete form with the store selected.
    – Alex
    Commented Mar 15, 2012 at 17:40
  • @Alex Sorry I missed that in the question...I would've thought you need to pass $form in by reference to the reload() function: function reload(&$form, &$form_state) {.... Otherwise the original form element won't be updated and the value selected will not be a valid one as far as the form API is concerned
    – Clive
    Commented Mar 15, 2012 at 17:47
  • @Clive adding function reload(&$form, &$form_state) makes no difference, still giving me the 'field is required error' like no value was selected
    – Alex
    Commented Mar 15, 2012 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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