Original http://hellodrupal.info/node/149
Module file structure:
- Sitemode.info
- Sitemode. Module
- Test_ajax.js
Sitemode.info
; $ ID $ name = sitemoddescription = a content type for jokes. Package = pro Drupal developmentcore = 6.x
Sitemode. Module
<? PHP Function Sitemod_menu (){ // Create a module sitemod $ Items = Array (); $ Items ['Ajax '] =Array ( 'Page callback' => 'sitemod _ callback_ajax ', 'Access arguments' => Array ('Access content'), 'type' => menu_callback, ); $ Items ['Test/ajax '] = Array ( // For the effect of calling form, see 'Page callback' => 'drupal _ get_form ', 'page arguments' => Array ('Get _ ajax_form '), // Get the defined form 'Access arguments' =>Array ('Access content'), 'type' => menu_callback, ); Return $ Items ;} Function Get_ajax_form (){ // Select form Drupal_add_js (drupal_get_path ('module ', 'sitemod').'/test_ajax.js '); // Load JS files $ Form ['Note _ Book'] = Array ( '# Type' => 'radios', '# title' => T ('select category'),' # default_value '=> 'ibm', '# options' => Array (T ('ibm '), T ('Dell'), T ('sony '), T ('hp ')), '# description' => T ('select your preferred brand '), ); Return $ Form ;} Function Sitemod_callback_ajax (){ $ ID = $ _ Post ['Id']; // Ajax Post Data Switch ($ ID ){ Case 0: Drupal_json ( Array ('Html '=> drupal_get_form ('ibm _ form '))); // Josn data. Form Exit ; Break ; Case 1: Drupal_json ( Array ('Html '=> drupal_get_form ('Dell _ Form'))); Exit ; Break ;}} Function Ibm_form (){ // Definition form $ Form ['Ibm '] = Array ( '# Type' => 'checkboxes',' # title' => T ('ibm's latest computer models '),' # default_value '=> Array ('T410'), '# options' => Array ( 'T400' => T ('t400'), 't410' => T ('t410'), 'x200' => T ('x200 '), 'x201' => T ('x201'), 't410s' => T ('t410s '), ) , '# Description' => T ('select your IBM Model '), ); $ Form ['Submit '] = Array ( '# Type' => 'submit', '# value' => T ('Save selected information'),' # weight' => 40, ); $ Form_state ['Redirect'] = 'test/view' ; Return $ Form ;} Function Dell_form (){ // Definition form $ Form ['Dell '] = Array ( '# Type' => 'checkboxes',' # title' => T ('Dell pcs'), '# default_value' => Array ('D1 '),' # options' => Array ( 'D1 '=> T ('d1'), 'd2 '=> T ('d2'), 'd3 '=> T ('d3 '), ) , '# Description' => T ('select your Dell model '), ); $ Form ['Submit '] = Array ( '# Type' => 'submit', '# value' => T ('Save selected information'),' # weight' => 40, ); Return $ Form ; // Return form }
Test_ajax.js
If (Drupal. jsenabled) {$ (document). Ready ( Function () {$ ( 'Input: Radio [name = note_book] '). Click ( Function (){ VaR Getsubmit = Function (Data) {$ ( '# Footer' ).Html(data.html);} $. Ajax ({type: 'Post' , URL: '/Drupal/ajax' , Datatype: 'Json' , Success: Function (MSG ){ // Alert (MSG ); $ ("# Main" Pai.prepend(msg.html);}, data :{ 'Id': $ ('input: Radio [name = note_book]: checked' ). Val (),},});});});}
End
End