當用戶點擊一個按鈕(帶有id
圖)時,我想用默認實例填充默認的Drupal內容div
(<div class="content">
)。如何在AJAX請求期間「打印」一個主題(Drupal)
中的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);
}
});
});
});
的PHP:
$items['publications/callback'] = array(
'type' => MENU_CALLBACK,
'title' => 'All Publications Callback',
'page callback' => '_process_publications',
'page arguments' => array(t('journal')),
'access callback' => TRUE,
);
這導致了頁面的回調:(我關心if
代碼塊)
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>");
}
}
和最後在回調函數中調用的函數:
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>");
}
}
問題是:返回的主題並沒有真正發送任何回AJAX請求(不像print
語句)。我試圖打印主題,但這會產生死亡的白色屏幕。我將如何生成圖形而不打印某些內容?
你試過drupal_json_output()嗎? –
不,我沒有,但我現在試試 – Don
這隻輸出JSON格式的'$ graphael'參數;它實際上並不生成圖 – Don