How to Use Ajax in form alter in Drupal 7? I have created a content type named "main menu". I have few select lists which depend on one another. I wanted to display a different list when different checkboxes are selected. I figured out that I need to use Ajax to edit the form in order to achieve that but can anyone tell me how to use Ajax with hook_form_alter?

The form id of the form I am supposed to edit is "main_menu_node_form". When I try to access this form using hook_form_alter(), and add the #ajax property of the form fields, it seems that I am not able to do that since the fields are already rendered before the handle is passed to hook_form_alter(). Can anyone please help me solve this?

Note : I have created the content type using hook_install().

function main_menu_content_type_form_main_menu_node_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  dpm($form);
  dpm($form_state);

  $form['field_mlnk'] = array(
    '#ajax' =&gt; array(
      'callback' =&gt; 'main_menu_content_type_form_main_menu_node_form_alter_callback',
      'wrapper' =&gt; 'link-div',
      'method' =&gt; 'replace',
      'effect' =&gt; 'fade',
    ),
  );
}
I would be able to give you some detailed information about what you are doing wrong with hook_form_alter if you included some actual code from your custom module. However, since you are not giving any specifics, you can watch the video: How to build beautiful forms in Drupal 7 (note that I entered the URL so that it starts at the moment where the ajax is described). About 5 minutes of watching this will probably give you the answer to your question (since it describes exactly how to use ajax with hook_form_alter), but if you are really impatient, the actual code is shown somewhere around here. 

If you want to see a live, well-tested and documented example of a form being altered using ajax, there is one in the Examples module. You can find the actual hook here and the callback function here.

I hope this helps. If it doesn't, give us some more background about your code and we will be able to tell you exactly what you are doing wrong.