I want to load a form via ajax and submit it by ajax.
But it not working.
my code:
【It is a wrong way to wast my so lost of time!!!!!!!!!! I hope someone else not do this through this way,so many unknowed problem】

1.add the library

function promycustom_preprocess_page(&$variables, $hook) {
  drupal_add_library('system', 'drupal.ajax');
  drupal_add_library('system', 'drupal.form');
}

2.hook menu

function mycustom_menu(){
$items=array();
$items['test/page']=array(
          'title'=>'ajax test',
          'description'=>'this is the ajax test',
          'page callback'=>'mycustom_ajax_test',
          'access callback'=>TRUE,
          'type'=>MENU_CALLBACK,
          );
return $items;
}

3.page call back,load the form and drupal setting

function mycustom_ajax_test(){
   $form= render(drupal_get_form('mycustom_change_user_brief_form'));
 
  $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 $form . $settings;
}

4.form build

function mycustom_change_user_brief_form($form, &$form_state){
  $form['abgegw']=array(
      '#markup'=>'welcome load ajax',
    );

  $form['submit'] = array(
     '#type' => 'submit',
    '#value' => 'change brief',
    '#weight' => 20,     
     '#ajax'=>array(
        'callback'=>'ajax_user_brief_handler',
        'wrapper'=>'content',
        'method'=>'html',
        'effect'=>'fade',
      ),
   );
  return $form;
}

5.my simple ajax call back

function ajax_user_brief_handler($form, &$form_state){
  $output='<div id="user-brief"> ajax handler success</div>'; 
  return $output;
}

6.in any other page load the form via ajax
the click button in any other page

click to load form via ajax

the js code

(function ($) {
Drupal.behaviors.mycustom = {
attach: function () {
$(".ajax-load").click(function(){
("#screen").load('/test/page #mycustom-change-user-brief-form',function(){Drupal.attachBehaviors();});
});
});
};
})(jQuery);

when visit the page /test/page, the ajax submit work well.
But when load the form via ajax in any other page,it only can click to load the form via ajax,but can't submit by ajax, when click the submit button it will go to the /test/page,
I have search for long time to find how to solve it ,but still can work it out.
How to solve it???any help will be full of thanks!!!!!!!

Comments

nevets’s picture

I would look at one of the existing modules that load forms by ajax.

And the ctools module includes an api for doing this.

315redad’s picture

How to do it use Ctool api please? thank you!

nevets’s picture

They have documentation on this and some of the existing modules that provide this functionality use ctools.

Jaypan’s picture

Change this:

  return $form . $settings;

To this:

  print $form . $settings;
  die;

Edit: and change this:

("#screen").load('/test/page #mycustom-change-user-brief-form',function(){Drupal.attachBehaviors();});

To this:

$("#screen").load('/test/page',function(){Drupal.attachBehaviors();});
315redad’s picture

Thank you Jaypan,I have change these two place , and it still can't submit by ajax. when click submit button,it still will jump to the /test/page.

nevets’s picture

From Jaypan's comment I realize you should look at the developers example module which includes examples that should help. In general, you need to utilize the Drupal api's for ajax.

Jaypan’s picture

Use a JavaScript console to examine the response from the server. Is the script tag with the settings included in the response?

Question still stands.

Ayesh’s picture

I would definitely go for the ctools module for this.

Ctools modal example module contains a nice example on how to do this.

When you load the form, you need to render() it, so Drupal can send additional data back to the page. In this form, the submit button needs to contain #ajax to handle the Ajax call.