Chinaunix首页 | 论坛 | 博客
  • 博客访问: 334895
  • 博文数量: 89
  • 博客积分: 5152
  • 博客等级: 大校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2006-02-25 15:12
文章分类

全部博文(89)

文章存档

2012年(1)

2011年(5)

2010年(14)

2009年(69)

我的朋友

分类: 系统运维

2009-06-26 11:02:45


We can easily use AJAX in DRUPAL framework. Drupal provide the jQuery javascript library so we can use jQuery for our AJAX implementation. First we write a module in which we are going to implement the server side logic. Suppose our module name is product and we will check the given product name is exist or not in our product table.


<?php
/*product.module*/
 
function product_menu() {
      $items = array();
 
    $items['product'] = array(
        'page callback' => 'drupal_get_form',
        'page arguments' => array('product'),
            'access arguments' => TRUE,
            'type' => MENU_CALLBACK,
      );
 
      $items['product/check_name'] = array(
            'page callback' => 'check_name',
            'access arguments' => TRUE,
            'type' => MENU_CALLBACK,
      );
 
      return $items;
}
 
function product() {
    $path = drupal_get_path('module', 'product');
    drupal_add_js($path . '/product.js', 'module');
 
    $form['product_name'] = array(
        '#title' => t('Product Name'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#size' => 30,
        '#description' => t('Please enter product name.'),
    );
 
    $form['check_name'] = array(
        '#type' => 'markup',
        '#value' => "" . t('Check Product Name') . "
"
,
    );
 
    $form['status'] = array(
        '#type' => 'markup',
        '#value' => "
"
,
    );
 
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
    );
 
    $form['cancel'] = array(
        '#type' => 'markup',
        '#value' => l(t('Cancel'), 'product_mgmt'),
    );
    return $form;
}
 
function check_name() {
    $name = strtolower($_GET['name']);
 
    $query = "SELECT COUNT(*) AS total FROM {product} WHERE LOWER(product_name) LIKE ('%s')";
    $rs = db_query($query, $name);
 
    $info = db_fetch_object($rs);
    $total = $info->total;
 
    if ($total) {
        echo "$('#status').html('This product is available.');";
    }
    else {
        echo "$('#status').html('This product is not available.');";
    }
}
?>

Don’t return anything in “check_name()” function otherwise it will return the whole page when we access “product/check_name” path through AJAX.

/*product.js*/
 
// JavaScript Document

$(document).ready(function() {
    $('#check_name').attr('href', 'javascript:void(0);');
 
    $('#edit-product-name').keydown(function(event){
        $('#status').html('');
    });
 
    $('#check_name').click(function() {
        var name = $.trim($('#edit-product-name').val());
 
        if (name == '') {
            $('#status').html('Please enter product name.'); return;
        }
 
        $.ajax({
               type: "GET",
            url: "/product/check_name",
               data: "name=" + encodeURI(name),
               success: function(msg){
                eval(msg);
               }
         });
    });
});

阅读(1057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~