By teebo on
Hi everyone,
I have a simple module, but it's not functioning. I cannot seem to get any JSON data to be received/sent correctly.
Here is the code:
artplatform.module:
// $Id$
/**
*@file
*Art Platform
*/
/**
* Implementation of hook_perm().
* Permissions hook function
*/
function artplatform_perm() {
return array('search artwork');
}
/**
*Implementation of hook_menu().
*/
function artplatform_menu() {
$items['artwork/searchartwork'] = array(
'page callback' => 'searchartworkcontent',
'access arguments' => array('search artwork'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Search DB for artwork
*/
function searchartworkcontent() {
$termIDvar = 0;
$nodeIDvar = '';
if (!empty($_POST['artworkType'])) {
$termIDvar = $_POST['artworkType'];
$nodeIDvar = db_result(db_query("SELECT node.nid AS nid FROM node node INNER JOIN term_node term_node ON node.vid = term_node.vid WHERE (node.status <> 0) AND (node.type in ('artwork')) AND (term_node.tid = '".$termIDvar."')")); // Obtained via views module
return drupal_json(array('nodeIDvar'=>$nodeIDvar));
exit();
} else {
return drupal_json(array('nodeIDvar'=>'no search yet.'));
exit();
}
}
/**
*Create widget to display on the web page.
*/
function searchartworkcontent_widget() {
return theme('searchartworkcontent_widget');
}
/**
*Implementation of hook_theme().
*LetDrupalknowaboutourthemefunction.
*/
function artplatform_theme() {
return array(
'searchartworkcontent_widget' => array(
'arguments'=>array(),
),
);
}
/**
*Theme for artwork platform.
*/
function theme_searchartworkcontent_widget() {
// Load the JavaScript and CSS files.
drupal_add_js(drupal_get_path('module', 'artplatform') .'/artplatform.js');
$output = '<div class="search_results"></div>';
return $output;
}
/**
* Implementation of hook_nodeapi().
* Show content in node
*/
function artplatform_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'view':
if(!$teaser){
$node->content['searchartworkcontent_widget']=array(
'#value' => searchartworkcontent_widget(),
'#weight'=>0,
);
}
break;
}
}
And the JS file artplatform.js:
// $Id$
//Only run if we are in a supported browser.
if(Drupal.jsEnabled) {
//Run the following code when the DOM has been fully loaded.
$(document).ready(function() {
$('div.search_results').fadeTo('slow', 0);
$('input:radio').click(function(){
$('div.artworkSearchResult').html('Sending...').fadeTo('slow', 0);
$.ajax({
type: 'POST',
url: this.href,
data: $('input:radio').serialize(),
dataType: 'json',
success: function(data) { alert(Drupal.t("Yes ") + data.nodeIDvar); $('div. search_results').html(data.nodeIDvar).fadeTo('slow', 1); },
error: function(xmlhttp) { alert(Drupal.t("An error occurred @ ") + xmlhttp.readyState); }
});
return false;
});
});
}
Via the ajax function I get a readystate of 4 and a status of 200 meaning that everything should be okay... but nothing is happening.
I have a list of taxonomy terms with radio buttons. When clicked, I want to get their value, and then display the $nid value for the nodes that contain the taxonomie term.
Any help would be much appreciated.
Comments
...
Install Firefox's Firebug and see what's your script's request and what's the server's response.