5

I have created a block through custom module. The content of the block should be random every time it loads the block. Call back function for this was written. The block also has a link say "Next" and when clicked on should reload the block with next random content.

How to get the block to reload without reloading the whole page when the link Next is clicked on?

Below is the module code I am using and js file.

/*
 * Implement hook_menu
 */

function my_module_menu(){
    $items['tips/reload']=array(
        'title' => 'Block Reload',
        'page callback'=>'reload_tip',
        'access callback' => TRUE,
        'type' => MENU_CALLBACK,
    );
    return $items;
}

/*
 * Implement hook_block_info().
 */
 function my_module_block_info(){
    $blocks['my_module']=array(
        'info' => t('My Tips'),
        'status' => TRUE,
        'region' => 'content',
        'weight' => 0,
        'visibility' => 0,
    );
    return $blocks;
 }



 /*
  * Implement hook_block_view().
  */
 function my_module_block_view($delta=''){
    switch($delta){
        case 'my_module':
            $block['subject'] = t('Teamie Tips');
            $block['content'] = my_module_block_content($delta);
            return $block;
        }

 }

 /*
  * tips block content function
  */
 function my_module_block_content($delta){
    $output = "<div id='divTip'>".getrandomtip()."</div>";
    $output .="<a id='NextTipLink' href='tips/reload'>Next</a>";
    return array('#markup'=>$output);
 }

  function reload_tip(){
    $tip = getrandomtip();

    return drupal_json_output(array('nexttip'=>$tip));
    exit;
 }

 function my_module_theme(){
    return array(
        'my_module_javascript'=>array('arguments'=>array(),),
    );
 }

 function my_module_init(){
    theme('my_module_javascript');
 }

 function theme_my_module_javascript(){
    $jsfile = drupal_get_path('module','my_module').'/my_module.js';
    drupal_add_js($jsfile);
 }

and my js file is as below

 (function ($) {Drupal.behaviors.my_module = function (context){
    $('a#NextTipLink').click(function(){
        var nextTip = function(data){
            $("#divTip").html(data.nexttip);
        }
        $.ajax({
            type:'POST',
            url:this.href,
            success:nextTip,
            data: 'js=1',
        });
        return false;
    });

}})(jQuery);

When I click on next link, pop up is coming with file having json object. But I need to reload the blok content. Am I doing something wrong?

6

To answer my own question below is the correct js file that is working.

(attach:function ($) {Drupal.behaviors.my_module = {function (context){
    $('a#NextTipLink').click(function(){
        var nextTip = function(data){
            $("#divTip").html(data.nexttip);
        }
        $.ajax({
            type:'POST',
            url:this.href,
            success:nextTip,
            data: 'js=1',
            dataType:'json',
        });
        return false;
    });

}};})(jQuery);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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