简体   繁体   English

使用jQuery使用数据库中的数据填充下拉列表

[英]Populate dropdown with data from database using jQuery

Error is seen when i made a drop down list to populate data from Database. 当我创建一个下拉列表以填充数据库中的数据时,看到错误。
Database : dtable 数据库:dtable
File in the template of hook_theme named : myModueForm_theme.tpl.php hook_theme模板中的文件名为: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 tag starts 脚本标签开始

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"); 
    }
})
}  
});  

script tag ends 脚本标签结束

The Module file which handles the database connection named : myModule.module 处理数据库连接的模块文件名为: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 //在search_data函数中

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 我想要的是
1-- please tell me where is the error & how to solve 1-请告诉我错误在哪里以及如何解决
2-- 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 . 2--我想将查询结果返回页面到主题php页面,但是当我显示模块文件的返回值时,我得到了整个HTML。

Please help & treat me as a beginner.Thanks in advance... /\\ 请帮助我,并将其视为初学者。请先感谢... / \\

The answer is :"Use ajax_deliver " the code will be: 答案是:“ Use ajax_deliver”,代码将是:

 $items['search/won'] = array( 
 'title' => 'Add data', //page title
 'description' => 'form',
'page callback' => 'search_won',
'page arguments' => array('access content'),
'delivery callback' => 'ajax_deliver',
'access callback' => TRUE
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM