Drupal Ajax JSON asynchronous call

Source: Internet
Author: User
Tags drupal

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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.