zoukankan      html  css  js  c++  java
  • drupal ajax json异步调用

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

    模块文件结构:

    • sitemode.info
    • sitemode.module
    • test_ajax.js

    sitemode.info

    ; $Id$
    name = Sitemod
    description = A content type for jokes.
    package = Pro Drupal Development
    core = 6.x

    sitemode.module

    <?php
    function 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(),
    
                },
            });
       
          });
          });
    }



     
  • 相关阅读:
    LeftoverDataException,依赖包,apache license 2.0
    GPL,BSD,Apache,MIT开源许可协议
    一次重构经历
    转载:reactor模式学习
    版本控制学习
    系统开发,出错处理,日志
    最近学习linux命令的一个总结
    sudo,linux 新建账号,并开通ssh登录
    运行R 报错R cannot R_TempDir, 继而发现/dev/mapper/VG00-LV01 磁盘空间已满
    用InputStream读出来转换成String类型
  • 原文地址:https://www.cnblogs.com/wangkangluo1/p/2789092.html
Copyright © 2011-2022 走看看