I followed this tutorial: https://rapiddg.com/blog/creating-ajax-callback-user-reference-fields

to create a button to fill some field in a form with the values of the current user.

Here the cose I added to my hoo_form_alter:

if ($form_id == 'datore_node_form')
global $user;
  $form['title']['#type'] = 'hidden';
  $form['title']['#value'] = 'test';

$form['user_replace'] = array(
    '#title' => t("User Information"),
    '#prefix' => '<div id="datore_node_form_group_cs">',
    //name of the node form (already existing, if you're modifying an existing node form)
    '#suffix' =>'</div>',

    $form['user_replace']['user_copy'] = array(
      '#type' => 'button',
      '#value' => 'Sono io il datore!',
      '#ajax' => array(
        'callback' =>'user_replace_ajax_callback',
        'wrapper' => 'datore_node_form_group_cs',

 $form['user_replace']['field_cognome'] = $form['field_cognome'];

  $user = user_load($user->uid);

  if (!empty($form_state['values']['field_cognome']))
    $form_state['input']['field_cognome']['und'][0]['value'] = $user->field_cognome['und'][0]['value'];
    return $form;

  function user_replace_ajax_callback($form, &$form_state)
    ajax_command_alert("hello"); // doesn't work
    return $form['user_replace'];

but if I click the button nothing happen. Any advise?

  • I can't really follow whats going on here, but if you are creating ajax commands you have to return a different response than the form item. You have to return an array of commands as type => '#ajax'
    – Kevin
    Commented Feb 16, 2017 at 19:20
  • Could you kindly provide an example? Commented Feb 17, 2017 at 14:14
  • First, get rid of all the dpm() calls. It will make debugging tougher for AJAX calls. Look into using xdebug. But in order to use ajax commands, your callback has to return an array of commands as an #ajax response type. See the callback function in this example: redcrackle.com/blog/… or the ajax callback here jaypan.com/tutorial/calling-function-after-ajax-event-drupal-7
    – Kevin
    Commented Feb 17, 2017 at 14:27

1 Answer 1


The only thing you are doing with your code is replacing the button that you are clicking. It's probably getting replaced, but visually will look exactly the same as it did before, which is why it appears nothing is happening.

  • OK, but under "['user_replace']" I have this: $form['user_replace']['field_cognome'], so the field I suppose should be filled, isn't it? Commented Feb 17, 2017 at 14:12
  • It's there on the initial load, it should be there on the ajax submit as well. You'll need to update your code to show where $form['field_cognome'] comes from to be able to understand.
    – Jaypan
    Commented Feb 17, 2017 at 14:15
  • $form['field_cognome'] comes from the original form I created with the UI Commented Feb 17, 2017 at 14:21
  • Looks like it should be the same either way to me. Open up a JS console, and examine the response to see if you are actually getting a response. Also, note that your alert will never happen, as another poster mentioned.
    – Jaypan
    Commented Feb 17, 2017 at 14:23
  • 1
    Also see Jaypans own post here for how to use AJAX commands in a callback jaypan.com/tutorial/calling-function-after-ajax-event-drupal-7
    – Kevin
    Commented Feb 17, 2017 at 14:27

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.