Hello,

On drupal 7, I have a problem with a form. This form is loaded by ajax and is submitted by ajax

If the form is not loaded by ajax, the submit works but if the form is loaded by ajax, the submit by ajax doesn't work.

Here my code :

// ../modules/custom/formation/panier.inc

function formation_panier($form, &$form_state) 
{
    form_load_include($form_state, 'inc', 'formation', 'panier');

    $panier_formations = isset($_SESSION['panier_formations']) ? $_SESSION['panier_formations'] : array();

    $form['title'] = array(
                    '#markup' => '<h2 class="block-title">Votre sélection de formations <div id="btn-close-cart"><i class="fa fa-times"></div></i></h2>',
                    '#weight' => -50,
                );
    $form['help'] = array(
                    '#markup' => '<div class="aide">Valider votre sélection pour vous inscrire aux formations</div>',
                    '#weight' => -49,
                );

        $form["profil"] = array(
            '#type' => 'radios',
            '#title' => 'Choisissez votre profil',
            '#options' => _formation_taxonomy_to_select_options('profils', NULL),
            '#default_value' => isset($_SESSION['mon_profil']) ? $_SESSION['mon_profil'] : NULL,
            '#required' => TRUE,
        );

        $form['submit_container_start'] = array(
            '#markup' => "<div class=\"submit_container\">",
            '#weight' => $i + 10,
        );


    // ****************** submit form by ajax *******************       
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Valider ma sélection',
        '#ajax' => array(
                'callback' => 'formation_panier_submit_ajax_submit',
                'wrapper' => 'formation-panier',
                'method' => 'html',
                'path' => 'system/ajax',
            ),
        '#weight' => $i + 11,
    );


    $form['submit_container_end'] = array(
        '#markup' => "</div>",
        '#weight' => $i + 12,
    );
return $form;

}

I create a page to display form. the form is displayed correctly and the submit by ajax is good.

// ../modules/custom/formation/formation.module

function formation_menu() {
    $items = array();

    $items['chargement-formation-panier'] = array(
    'title' => 'Ma sélection de formations',
    'access arguments' => array('access content'),
    'page callback' => '_formation_chargement_panier',
    'file' => 'panier.inc',
    );

    $items['gestion-panier'] = array(
    'title' => 'Gestion de ma sélection de formations',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('formation_panier'),
    'access arguments' => array('access content'),
    'file' => 'panier.inc',
    );

    return $items;
}

Now, I load the form by ajax

// ../modules/custom/formation/formation.js

Here, the javascript to load the form by ajax

$('#cta-panier-formations').click(function(e) {
    e.preventDefault();
    var $panier = $('#panier-formations');
    var $body = $('body');
    if ($body.hasClass('panier-formations-ouvert'))
        $body.removeClass('panier-formations-ouvert');
    else {


        //********* on charge en ajax le panier *************
        $.get('/chargement-formation-panier', {}, function(jsondata) {
            //Add the html to the div
            $('#panier-formations').html(jsondata);

            Drupal.attachBehaviors();

        }, "html");

        $body.addClass('panier-formations-ouvert');
    }

});

// ../modules/custom/formation/panier.inc

function _formation_chargement_panier()
{ 
    print _formation_reload_panier(true); drupal_exit();
}

function _formation_reload_panier($ajax = true)
{
    $form = drupal_get_form('formation_panier');
    $form = drupal_render($form);

    if($ajax){
        $settings = FALSE;
        $javascript = drupal_add_js(NULL, NULL);
        if(isset($javascript['settings'], $javascript['settings']['data']))
        {
            $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
            $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $javascript['settings']['data']));
            $settings .=  ');</script>';
        } 
        //return $html . $settings;
        print $form . $settings; die();
    }
    else {
        return $form;
    }
}

The form is correctly loaded by ajax but the submit doesn't work.

// ../modules/custom/formation/formation.module

function formation_init() {
    drupal_add_js('misc/ajax.js');
    drupal_add_js('misc/jquery.form.js');
    drupal_add_js(drupal_get_path('module', 'formation') . '/formation.js', array('scope' => 'footer', 'weight' => 55));
}

Thanks,
Phil