Drupal Form Ajax not updating form on first click function _ajax_ticket_info($form,&amp;$form_state)
{
    $subscriber = $form['info']['ticket']['subscribe'];
    _ticket_subscribe($subscriber['#value'],$subscriber['#name']);

    // Dunno if this is needed or not
    $form_state['rebuild'] = TRUE;


    return $form;
}
function _ticket_info($form,&amp;$form_state,$node) {

    $is_subscribed = _ticket_is_subscribed($node-&gt;nid);
    $subscriber_count = count(_ticketsys_get_subscribers($node));

    $form = [
        '#prefix' =&gt; "&lt;div id='ticket-info-form'&gt;",
        '#suffix' =&gt; '&lt;/div&gt;',
    ];
    // other form fields here not that important 
    // just focusing on my problem 
    $form['info']['ticket']['subscribe'] = [
    '#type' =&gt; 'button',
    '#value' =&gt; $is_subscribed,
    '#name' =&gt; $node-&gt;nid,
    '#id' =&gt; 'ticket-subscribe',
    '#ajax' =&gt; [
        'callback' =&gt; '_ajax_ticket_info',
        'wrapper' =&gt; 'ticket-info-form',
        'effect' =&gt; 'fade',
    ],
];
return $form;
}


So the problem is i'm using that code to update my form without reloading the page and when i do that it only executes the replacement on the second time i clicked the button $form['info']['ticket']['subscribe'] and i notice using inspect element on chrome that the form id and other form fields have --2 on their ids and increasing every time i click the Subscribe button

Any ideas whats wrong here. I really need to fix this thanks
Try changing the button type to "submit", and specifying a custom submit handler:

$form['info']['ticket']['subscribe'] = [
    '#type' =&gt; 'submit',
    '#submit' =&gt; [['ticket_submit_handler']]
    '#ajax' =&gt; [
        'callback' =&gt; 'ticket_ajax_callback',
        'wrapper' =&gt; 'ticket-info-form',
    ],

    // ...
];


In the submit handler:


Change any variables in form_state storage that need to persist.
Tell the form to rebuild itself (instead of doing a redirect after submission), in order to keep the form state.
Do other stuff.


e.g.

function ticket_submit_handler(&amp;$form, &amp;$form_state) {
    $form_state['storage'][] = ;
    $form_state['rebuild'] = TRUE;
    // ...
}


In the AJAX callback:


Return the part of the form that will replace the 'wrapper'. In your case, it seems to be the entire form.
Don't do anything else here.


e.g.

function ticket_ajax_callback(&amp;$form, &amp;$form_state) {
    return $form;
}


AFAIK, when the user clicks your AJAX button, the following functions will run, in order:


Form validation (if any).
ticket_submit_handler
Form builder, to rebuild the entire form, even if we only need a part.
ticket_ajax_callback, which returns a part of the form, which is sent to the browser.


For more information, see the example code in the Drupal 7 documentation.