-
ajax in drupal
over 8 years ago
-
over 8 years ago
As you enter the student name call a javascript function to execute the ajax call then, Set the element that will receive the AJAX callback, like:
<div id="ajax-target">Ajax goes here!!!</div>
After that goto the custom code:
1--> Create a JavaScript function that will handle the call using jQuery.load(). The $nid in the path is just an example. It could be any variable.
<script> function myModule_ajax_load(name) { jQuery("#ajax-target").load("/node/ajax/"+name); } </script>
Note: please use _ in place of _ in above code
Write code in custom module:2--> Register the URL that will render the HTML by implementing hook_menu():
function myModule_menu() { $items['node/ajax/%'] = array( 'page callback' => 'myModule_ajax_get_ajax', // Render HTML. 'page arguments' => array(2), 'type' => MENU_CALLBACK, 'access arguments' => array('access content'), 'delivery callback' => 'myModule_ajax_callback', // Magic goes here. ); return $items; }
3--> Create page callback and delivery callback functions:
function myModule_ajax_get_ajax($nid) { // You can return whatever you want, including forms. $node = node_load($nid); return node_view($node, 'teaser'); } function myModule_ajax_callback($page_callback_result) { // Only render content $content = drupal_render($page_callback_result); // Add CSS and JS files, add some html $html = '' . drupal_get_css() . drupal_get_js() . '' . $content . ''; print $html; // Perform end-of-request tasks. drupal_page_footer(); }
-
1 Answer(s)