Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:28:47+00:00 2026-06-06T18:28:47+00:00

When users click on a button (with id graph), I’d like to fill the

  • 0

When users click on a button (with id graph), I’d like to fill the default Drupal content div (<div class="content">) with a graphael instance.

The JavaScript:

jQuery(document).ready(function($) {

   $('#toggle #graph').click(function() {


    $.ajax({
      url: "http://www.mysite.com/?q=publications/callback",
      type: 'POST',
      data: {
        'format' : 'graph'
      },
      success: function(response) {
        $('div#content div.content').html(response);
      }
    });
  });
});

The PHP:

$items['publications/callback'] = array(
  'type' => MENU_CALLBACK,  
  'title' => 'All Publications Callback',
  'page callback' => '_process_publications',
  'page arguments' => array(t('journal')),
  'access callback' => TRUE,
);

which leads to the page callback: (I’m concerned with the if code block)

function _process_publications($venue) {

if( isset($_POST['format']) && $_POST['format'] == "graph" ){
    _make_bar_chart($venue);
}
elseif( isset($_POST['format']) && $_POST['format'] == "list" ) {
    _make_list($venue);
}
else{
    return("<p>blah</p>");
}

}

and finally the function called within the callback function:

function _make_bar_chart($venue) {

// get active database connection 
$mysql = Database::getConnection();

// if connection is successful, proceed
if($mysql){
       // do stuff


        $graphael = array(
            'method' => 'bar',
            'values' => $ycoordinates,

            'params' => array(
              'colors' => $colors,
              'font' => '10px Arial, sans-serif',
              'opts' => array(
                'gutter' => '20%',
                'type' => 'square',
              ),
              'label' => array(
                'values' => $xcoordinates,
                'isBottom' => true,
              ),
            ),

            'extend' => array(
              'label' => array(
                'values' => $ycoordinates,
                'params' => array('attrText' => array(
                  'fill' =>  '#aaa',
                  'font' => '10px Arial, sans-serif',
                )),
              ),
            ),
        );

    return theme('graphael', $graphael);

}

// else, connection was unsuccessful
else{
    print("<p>bad connection</p>");
}

} 

THE PROBLEM: returning a theme doesn’t really send anything back to the AJAX request (unlike print statements). I tried to print the theme, but that produces a white screen of death. How would I generate the graph without printing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:28:49+00:00Added an answer on June 6, 2026 at 6:28 pm

    Much thanks to nevets on the Drupal forums for the helpful hint: http://drupal.org/node/1664798#comment-6177944

    If you want to use AJAX with Drupal, you are best off actually using Drupal-specific AJAX-related functions. In my theme’s page.tpl.php file, I added the following to make the links which would call AJAX:

    <?php
        // drupal_add_library is invoked automatically when a form element has the
        // '#ajax' property, but since we are not rendering a form here, we have to
        // do it ourselves.
        drupal_add_library('system', 'drupal.ajax');
    
    
            // The use-ajax class is special, so that the link will call without causing
            // a page reload. Note the /nojs portion of the path - if javascript is
            // enabled, this part will be stripped from the path before it is called.
            $link1 = l(t('Graph'), 'ajax_link_callback/graph/nojs/', array('attributes' => array('class' => array('use-ajax'))));
            $link2 = l(t('List'), 'ajax_link_callback/list/nojs/', array('attributes' => array('class' => array('use-ajax'))));
            $link3 = l(t('Create Alert'), 'ajax_link_callback/alert/nojs/', array('attributes' => array('class' => array('use-ajax'))));
    
            $output = "<span>$link1</span><span>$link2</span><span>$link3</span><div id='myDiv'></div>";
            print $output;
    
    ?>
    

    When one of the links above is clicked, the callback function is called (e.g. ajax_link_callback/graph):

    // A menu callback is required when using ajax outside of the Form API.
      $items['ajax_link_callback/graph'] = array(
       'page callback' => 'ajax_link_response_graph',
       'access callback' => 'user_access',
       'access arguments' => array('access content'),
       'type' => MENU_CALLBACK,
      );
    

    .. and the callback to which it refers:

    function ajax_link_response_graph($type = 'ajax') {
      if ($type == 'ajax') {
       $output = _make_bar_chart('journal');
       $commands = array();
       // See ajax_example_advanced.inc for more details on the available commands
       // and how to use them.
       $commands[] = ajax_command_html('div#content div.content', $output);
       $page = array('#type' => 'ajax', '#commands' => $commands);
       ajax_deliver($page);
      }
     else {
       $output = t("This is some content delivered via a page load.");
       return $output;
     }
    }
    

    This replaces any HTML within <div class="content"> with the graphael chart returned from _make_bar_chart above.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jqGrid on a page and users can click a button to
In my Swing app, users can click a button to open a dialog panel
We want to let users click a thumbs up or thumbs down button from
On document ready I run this code: jQuery(document).ready(function(){ jQuery('#button').click(function() { jQuery('#contact_form').load(/Users/mge/Downloads/jquery-ajax-1/readme.txt); return false; });
I have done like if user click on the Button then the Sound is
I have a div that I want when user click on a button slideUp
My web page has several dropdowns, users can click through the next button to
Right now, users click a button with JQuery. It dynamically adds a new form
Distilling this project down to the simplest of terms; Users click a button, a
My gridview is bound to List when users click a refresh button as follows:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.