0

I am in the process of converting Drupal 6 module to Drupal 7.
i have a problem of Passing php data to Ajax.
The function should display if a speaker on or off by interchanging icons, when clicked by using (toggle function).
whats i have discovered is that this part $.ajax() doesnt work, while in Drupal 6 it works.
below is the code.

longa.module file:

  //hook menu
  function longa_menu(){
  $items=array();
  $items['longa/set_speaker_state'] = array(
   'page callback' => 'longa_set_speaker_state',
   'access arguments' => array('longa talk'),
   'type' => MENU_CALLBACK,
  );
  return $items;
}
 //the callback
 function longa_set_speaker_state($speaker_state) {
  global $user;
  $_SESSION['longa_speaker_state'] = $speaker_state;
  user_save($user, array('longa_speaker_state' => $speaker_state));
 }

longa.js file:

(function($){
 $(document).ready(function(){
  if (Drupal.settings.longa_module.has_audio ==1){    
  im_flash_player_listener.onInit = function(){
    this.position = 0;
  };
  if ($('#im-console-speaker-icon-on').css('display') == 'none') {
    is_muted = 1;
  }
  else {
    is_muted = 0;
  }
  $('#im-console-speaker-icon-on').click(function() {
    set_speaker_state(1);
    is_muted = 1;
    //alert('You clicked the speaker');
    $('#im-console-speaker-icon-on').toggle();
    $('#im-console-speaker-icon-off').toggle();
    });
  $('#im-console-speaker-icon-off').click(function() {
    set_speaker_state(1);
    is_muted = 0;
    $('#im-console-speaker-icon-on').toggle();
    $('#im-console-speaker-icon-off').toggle();
    });
  }

 });
})(jQuery);

 function set_speaker_state($speaker_state) {
  if (Drupal.settings.longa_module.has_perms ==0) {
   return;
  }
 var sendurl= url_prefix + 'longa/set_speaker_state/' + $speaker_state;
  //alert(sendurl);
   $.ajax({
   type: 'GET',
   url: sendurl ,
   dataType: 'json',
   data: 'js=1'
   /**success: function(xhr) { 
    alert('sent successfully');
    },
    error:function(xhr) { 
   alert('error occured');
   }*/
 });
}

I will appreciate for any help.

2 Answers 2

1

In function set_speaker_state() , the $ symbol doesn't recognized, you must put set_speaker_state() function into:

(function($, window, undefined){
 // here!
})(jQuery, window);
1
  • i did as u suggested, removed the jQuery wich i added.. its working. Thank you.
    – mrcniceguy
    Commented Dec 8, 2012 at 7:56
0

Finally i got it working...i just replaced $.ajax with jQuery.ajax and bum, everything works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.