1

Error is seen when i made a drop down list to populate data from Database.
Database : dtable
File in the template of hook_theme named : myModueForm_theme.tpl.php

<?php
$sel = "<select  name=\"select1\" id=\"select1\">
        <option value=\"\">Select...</option>
        <option value=\"data1\">data1</option>
        <option value=\"data2\">data2</option>
        <option value=\"data3\">data3</option>
        <option value=\"data4\">data4</option>
    </select>";
echo "$sel";

?>

Script :

jQuery("#select1").change( function(){
if( jQuery(this).val() == "data1" ) {
jQuery.ajax({
    type: "POST",
    url: "<?php echo $base_url;?>?q=search/data1",
    error: function(returnval) {
        alert("Failure");
    },
    success: function (returnval) {
        alert(returnval);
        alert("Sucess"); 
    }
})
}  
});  

The Module file which handles the database connection named: myModule.module

$items['search/data1'] = array() 
'title' => 'Add data', //page title
'description' => 'form',
'page callback' => 'search_data1',
'page arguments' => array('access content'),
'access callback' => TRUE

);

// In the search_data function

function search_data1(){  

$options="<select  name=\"select2\" id=\"select2\">"; 
$result = db_select('dtable', 'c')
    ->fields('c')
    ->execute() 
    ->fetchAll();
foreach ($result as $record) {
$name = ($record->dt1);
    $options.="<option value=\"$name\">".$name."</option>"; 
    //alert(node);
    
} 
$options.= "</SELECT>";
return $options;

}

What I want is

  • please tell me where is the error & how to solve

  • I want to return the page back to the theme PHP page with the query result, but when I display back the return value from the module file, I get the whole HTML.

    Please help & treat me as a beginner.Thanks in advance...

  • Can you post the Javascript error? Also what is it you're trying to do? Are you sure you need to do this through Javascript? – Rick Dec 1 '14 at 16:51
  • @Richard I want to load the dropdown box with the database contents. b ut when i returm the value from the "module" , iam getting full HTML content & not the required database output. – Syamili V Dec 2 '14 at 1:47
1

By default, hook_menu's delivery callback is set to drupal_deliver_html_page(), therefore it delivers content wrapped in a page template. You will need to modify the delivery callback.

To output page content in ajax

function MYMODULE_menu() {
  $items['search/data1'] = array() 
  'title' => 'Add data', //page title
  'description' => 'form',
  'page callback' => 'search_data1',
  'page arguments' => array('access content'),
  'delivery callback' => 'MYMODULE_ajax_deliver',
  'access callback' => TRUE
}

function MYMODULE_ajax_deliver() {
  if ($page_callback_result) echo drupal_json_output($page_callback_result);
}

To output naked page content

function MYMODULE_menu() {
  $items['search/data1'] = array() 
  'title' => 'Add data', //page title
  'description' => 'form',
  'page callback' => 'search_data1',
  'page arguments' => array('access content'),
  'delivery callback' => 'MYMODULE_custom_deliver',
  'access callback' => TRUE
}

function MYMODULE_custom_deliver($page_callback_result) {
  if ($page_callback_result) echo $page_callback_result;
}

To learn more about a custom delivery callback function, see my other answer.

| improve this answer | |
  • but the link you provided , the answer is not dealing with ajax_deliver. Please reply – Syamili V Dec 2 '14 at 8:01
  • I've updated the answer. In your case it's probably better that you don't use ajax_deliver. – timofey.com Dec 3 '14 at 1:12
  • If you'd like to convert the page into ajax, follow my first example (it's updated). Just add 'delivery callback' => 'MYMODULE_ajax_deliver' to your hook_menu and add a function MYMODULE_ajax_deliver() to convert the page into JSON. – timofey.com Dec 3 '14 at 1:13

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.