drupal ajax json非同步呼叫

來源:互聯網
上載者:User

原文http://hellodrupal.info/node/149

模組檔案結構:

  • 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

<?phpfunction sitemod_menu() {// 建立一個模組 sitemod  $items = array();  $items['ajax'] = array(    'page callback' => 'sitemod_callback_ajax',    'access arguments' => array('access content'),    'type' => MENU_CALLBACK,  );  $items['test/ajax'] = array(//調用form效果 見    'page callback' => 'drupal_get_form',    'page arguments' => array('get_ajax_form'),// 得到定義好的表單    'access arguments' => array('access content'),    'type' => MENU_CALLBACK,  );  return $items;}function get_ajax_form(){//返回form選選項  drupal_add_js(drupal_get_path('module', 'sitemod') . '/test_ajax.js');//載入js檔案  $form['note_book'] = array(    '#type' => 'radios',    '#title' => t('選擇分類'),    '#default_value' => 'IBM',    '#options' => array(t('IBM'), t('Dell'), t('Sony'),t('HP')),    '#description' => t('選擇你喜歡的品牌'),  );   return  $form;}function sitemod_callback_ajax() {$id = $_POST['id']; //ajax post資料switch($id){  case 0:    drupal_json(array('html' => drupal_get_form('ibm_form')));//josn資料。form     exit;     break;  case 1:   drupal_json(array( 'html' => drupal_get_form('dell_form')));      exit;     break;}}function ibm_form(){//定義表單  $form['ibm'] = array(  '#type' => 'checkboxes',   '#title' => t('IBM最新型號電腦'),   '#default_value' => array('T410'),  '#options' => array(    'T400' => t('T400'),     'T410' => t('T410'),     'X200' => t('X200'),    'X201' => t('X201'),    'T410S' => t('T410S'),  ),  '#description' => t('選擇你喜歡的IBM型號'),   );    $form['submit'] = array(    '#type' => 'submit',    '#value' => t('儲存選擇的資訊'),    '#weight' => 40,  );     $form_state['redirect'] = 'test/view';return $form;}function dell_form(){//定義表單  $form['dell'] = array(  '#type' => 'checkboxes',   '#title' => t('Dell電腦'),   '#default_value' => array('d1'),  '#options' => array(    'd1' => t('d1'),     'd2' => t('d2'),     'd3' => t('d3'),  ),  '#description' => t('選擇你喜歡的dELL型號'),   );      $form['submit'] = array(    '#type' => 'submit',    '#value' => t('儲存選擇的資訊'),    '#weight' => 40,  );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").prepend(msg.html);   },          data: {             'id': $('input:radio[name=note_book]:checked').val(),            },        });         });      });}

 

 

 

 

 完
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.