0

I have created one content type named "Project". I have added one field in it named "Skills". Skills is a multiple select field. And I have added one submit button. Which I want to use to get records of users. I have created an Ajax form submit, but my issue now is that I can't alter the form after the Ajax submit.

This is the code I'm currently using:

function MYMODULE_form_alter(&$form, $form_state, $form_id) {

  if ($form_id == 'project_node_form') {

    $form['field_user_lists']['#access'] = FALSE;

    // Provide a submit button.
    $form['submit'] = [
      '#type'   => 'submit',
      '#value'  => 'Get Employee',
      '#weight' => '4',
      '#ajax'   => [
        'callback' => 'MYMODULE_form_ajax_submit',
        'wrapper'  => 'project_node_form',
        'method'   => 'replace',
        'effect'   => 'fade',
      ],
    ];

    $form['actions']['submit']['#submit'][] = '_custom_form_submit';
  }
}

function MYMODULE_form_ajax_submit($form, &$form_state) {

  if (!empty($form_state['input']['field_skills']['und'])) {

    //print_r($form_state['input']['field_skills']['und']); die();
    $query = db_select('field_revision_field_skillss', 'n');

    $query->join('users', 'u', 'n.entity_id = u.uid'); //JOIN node with users
    $query->groupBy('u.uid');//GROUP BY user ID
    $query->fields('u', ['uid', 'name'])//SELECT the fields from node
    ->condition('n.field_skillss_tid', $form_state['input']['field_skills']['und'], 'IN');

    $result = $query->execute();

    $form['field_user_lists']['#access'] = TRUE;

    while ($record = $result->fetchAssoc()) {
      print_r($record['name']);
    }
  }
  else {
    $output = 'Enter a value';
  }
}
  • 1
    The implementation of #ajax callback is not correct. This is not a form submission, but a separate callback that returns either a render array or or an array of ajax commands (as noted by Quint below). The former is usually the pattern done and then the actual logic is still in the form or form alter. Your submit or validation callback could have some form state value or storage set, which your form callback or alter then interprets and does the logic/query. – mradcliffe Mar 20 at 11:55
0

I think you are getting close. When you use ajax in Drupal, you have to return a render array with commands. In your case it looks like you would want ajax_command_replace.

Two additional steps should get you up and running.

First, built an html string instead of using print_r to try and send content back to the client.

    $return_html = '<div class="myclass">';    
    while ($record = $result->fetchAssoc()) {
              $return_html.='<div class="my_user">' . $record['name'] . '</div>';
            }
    $return_html .= '</div>';

Then create the ajax object and render array to send back to the client

 $commands[] = ajax_command_replace('#my_selector', 'some html here');
 return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );

In this example, you would need to have a div on your page with an id of 'my_selector' that would receive the new html.

  • Thank for your help! It's working fine now.:) – Kritika Bajpai Mar 22 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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