Populate dropdown with data from database in Drupal7 using jQuery 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
&lt;?php
$sel = &quot;&lt;select  name=\&quot;select1\&quot; id=\&quot;select1\&quot;&gt;
        &lt;option value=\&quot;\&quot;&gt;Select...&lt;/option&gt;
        &lt;option value=\&quot;data1\&quot;&gt;data1&lt;/option&gt;
        &lt;option value=\&quot;data2\&quot;&gt;data2&lt;/option&gt;
        &lt;option value=\&quot;data3\&quot;&gt;data3&lt;/option&gt;
        &lt;option value=\&quot;data4\&quot;&gt;data4&lt;/option&gt;
    &lt;/select&gt;&quot;;
echo &quot;$sel&quot;;

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

The Module file which handles the database connection named: myModule.module
$items['search/data1'] = array() 
'title' =&gt; 'Add data', //page title
'description' =&gt; 'form',
'page callback' =&gt; 'search_data1',
'page arguments' =&gt; array('access content'),
'access callback' =&gt; TRUE

);
// In the search_data function
function search_data1(){  

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

}
What I want is

please tell me where is the error &amp; 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 &amp; treat me as a beginner.Thanks in advance...
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' =&gt; 'Add data', //page title
  'description' =&gt; 'form',
  'page callback' =&gt; 'search_data1',
  'page arguments' =&gt; array('access content'),
  'delivery callback' =&gt; 'MYMODULE_ajax_deliver',
  'access callback' =&gt; 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' =&gt; 'Add data', //page title
  'description' =&gt; 'form',
  'page callback' =&gt; 'search_data1',
  'page arguments' =&gt; array('access content'),
  'delivery callback' =&gt; 'MYMODULE_custom_deliver',
  'access callback' =&gt; 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.