使用 Jquery 显示数据库值而不重新加载页面:不起作用
我试图在不重新加载页面的情况下显示数据库中的信息。我的视图中有一个下拉选择选项,当我选择任何值时,下拉列表仅显示表标题,但不显示数据库中的数据。我努力想找出问题所在,但失败了。
当用户选择 时,我希望 jquery 将数据发送到此控制器 - (测试/索引),但我不不知道该怎么做。
您能帮我解决这些问题吗?下面是我的 jQuery、标记和 CodeIgniter 代码。
头部内部:
<script type="text/javascript" src="<?php echo base_url(); ?>support/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select[name='users']").attr("selectedIndex", 1);
$("select[name='users']").change(function() {
var str = $(this).val();
if( str == "" ) {
$("#txtHint").html("");
}
else {
$.get("<?php echo base_url(); ?>test/query/str", function(data) { $("#txtHint").html(data) });
}
}).change();
});
</script>
身体内部:
<form>
<select name="users" >
<option value="">Select a person:</option>
<option value="11080101">Sumon</option>
<option value="11080102">Donno</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
我的控制器:
function query($id){
$sql="SELECT * FROM attendancein WHERE attendeeid = '$id'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>date</th>
<th>In Time</th>
<th>In Status</th>
<th>Class Start Time</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['intime'] . "</td>";
echo "<td>" . $row['instatus'] . "</td>";
echo "<td>" . $row['classstarttime'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
I am trying to show information from database without reloading the page. I have a dropdown select option in my view, when I select any value, the dropdown only shows the table titles, but not the data from database. I tried hard to figure out the problem, but I failed.
And when users choose <option value="">Select a person:</option>
, I want the jquery to send data to this controller- (test/index), but I don't know how to do this.
Would you please kindly help me to solve these problems? Below is my jQuery, markup, and CodeIgniter code.
Inside Head:
<script type="text/javascript" src="<?php echo base_url(); ?>support/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select[name='users']").attr("selectedIndex", 1);
$("select[name='users']").change(function() {
var str = $(this).val();
if( str == "" ) {
$("#txtHint").html("");
}
else {
$.get("<?php echo base_url(); ?>test/query/str", function(data) { $("#txtHint").html(data) });
}
}).change();
});
</script>
Inside Body:
<form>
<select name="users" >
<option value="">Select a person:</option>
<option value="11080101">Sumon</option>
<option value="11080102">Donno</option>
</select>
</form>
<br />
<div id="txtHint"><b>Person info will be listed here.</b></div>
My Controller :
function query($id){
$sql="SELECT * FROM attendancein WHERE attendeeid = '$id'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>date</th>
<th>In Time</th>
<th>In Status</th>
<th>Class Start Time</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['intime'] . "</td>";
echo "<td>" . $row['instatus'] . "</td>";
echo "<td>" . $row['classstarttime'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您可能理解的那样,您没有返回任何数据,因为控制器没有收到任何参数。
我并不完全熟悉 CodeIgniter 中的路由,但它似乎是 REST 的一些风格。这很好,但是 jQuery 比标准 RESTful 路由更好地处理查询字符串。
但不用担心。这仅意味着您已将查询数据合并到 url 中,而不是让 jQuery 使用查询字符串发送它。
我想你想做一个像这样的 jQuery get() :
假设你有一个类似于这样的路由:
$route['test/query/(:num)'] = yourController:
As you probably understood, you're not returning any data because the controller isn't getting any arguments.
I'm not completely familiar with routing in CodeIgniter, but it seems to be some flavour of REST. This is nice, but jQuery plays a little bit nicer with query strings than standard RESTful routing.
But don't worry. That only means that you have merge your query data into the url, instead of having jQuery send it using a query string.
I think you want to do a jQuery get() like this:
This is assuming you have a route similar to this:
$route['test/query/(:num)'] = yourController: