The purpose of the code is just to make $.get and $.post ajax request on change event of the select element and make a callback to retrieve some values. It is not intended to do anything good. It is just a practice code.

It seems no $.get and $.post ajax calls are not achieved. Why?

//testjquery.js file is in /modulepath/
if (Drupal.jsEnabled){
$(document).ready(function(){

   if ($("#edit-name").val() > 0){
       selectchange($("#edit-name").val());}
});

function selectchange(namefieldvalue) {
  $.get("test", { func: "getname" }, function(data){
	  var result = Drupal.parseJson(data);
	  $('div.name').html(result['name']);});
}
}

<?php

function testjquery_menu($may_cache) {
  $items = array();
  if($may_cache){
      $items[] = array('path' => 'testjquery',
      'title' => t('Test Jquery'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('testjquery_form'),
      'access' => user_access('make testjquery'));
      $items[] = array(
      'path' => 'test',
      'title' => t('Test Case'),
      'callback' => 'testjquery',
      'access' => user_access('make testjquery'));
     }  
  return $items;
}
function testjquery_form(){ 
  drupal_add_js(drupal_get_path('module','testjquery').'/testjquery.js');
  print_r( "<div class='name'></div>");
  $form['name'] =  array(  
     '#type' => 'select', 
     '#id' => 'edit-name',
     '#title' => t('Jquery Practice'),
     '#options' => array('option1','option2','option3'),
    '#attributes' => array('onchange' => 'selectchange(this.value);'),
  );
  return $form;
}
function testjquery() {
   print drupal_to_js(array('name'=>'JQuery Seems Good'));
}
........

Comments

nevets’s picture

If you use Firefox the firebug extension has a nice js debugger that is useful.

In the case of your code I think the problem probably lies in that your menu callback uses the path 'testjquery' and your call to $.get uses 'test'. (you may need a leading slash, ie '/testjquery')

wisdom’s picture

that is typo the "test" instead of "testjquery" and I also tried '/testjquery' and no return result. I will try with firebug if there is any hint but right now I am experiencing issue running firebug after upgrading to firefox 3.
Online Business

wisdom’s picture

It seems the post/get request is carried out upon a change of the select element. But I see the changes on the page only after refreshing the page. I do not see the ajax effect here.

Online Business

nevets’s picture

A couple of pointers. On the Drupal side in your callback you can use the watchdog() function to log information to see what the callback is doing. You might also want to use $.ajax() instead of $.get(). While a bit more work, it allows you to specify a callback for failures which is useful in cases where things are not working as expected.