尝试将查询结果显示在同一页面上
Try to display the query result to display on the same page
我又需要帮助了…我试图列出的主要类别列表,除了每个类别,有一个视图按钮,看到它的子和显示在同一页面内。下面是我的代码:
<table width="100%" align="center" style="margin-top:20px;">
<tr><td align="center"><font size="6">Categories</font></td></tr></table>
<?php
$sql = mysql_query("Select * FROM categories WHERE top_cat_id = 0")
?>
<div id="mainCat">
<form method="POST" id="viewSub" action="">
<table width="100%" align="center" class="categories">
<tr><td colspan="3" style="background-color:#000; color:#FFF; font-size:16px;">Main Categories</td></tr>
<tr><td align="center" width="10%" style="font-size:14px; font-weight:bold;">ID</td>
<td align="center" style="font-size:14px; font-weight:bold;">Categorie Name</td>
<td align="center" width="30%" style="font-size:14px; font-weight:bold;">Subcategories</td></tr>
<?php
while($data = mysql_fetch_array($sql))
{
echo '<tr><input type="hidden" name="cat_id" id="cat_id" value="'.$data['cat_id'].'"><td align="center">'.$data['cat_id'].'</td>';
echo '<td align="center">'.ucwords(strtolower($data['cat_name'])).'</td>';
echo '<td align="center"><input type="submit" name="submit" value="View" /></td></tr>';
}
?>
</table></form>
</div>
<script type="text/javascript">
$(function() {
$("#viewSub").bind('submit',function() {
var cat_id = $('#cat_id').val();
$.post('subcategories.php',{cat_id:cat_id}, function(data){
$("#subCat").html(data);
});
return false;
});
);
</script>
<div id="subCat">
</div>
如您所见,我猜jQuery函数只监听第一个View按钮,因为无论我点击哪个按钮,它只返回第一个主类别的子类别。有什么办法能让它成功吗?比如使用onclick操作?我不擅长javascript,所以我需要帮助,让这个工作…
谢谢。
下面是一个工作代码:
<table width="100%" align="center" style="margin-top:20px;">
<tr><td align="center"><font size="6">Categories</font></td></tr></table>
<?php
$sql = mysql_query("Select * FROM categories WHERE top_cat_id = 0");
?>
<div id="mainCat">
<!--<form method="POST" id="viewSub" action="">-->
<table width="100%" align="center" class="categories">
<tr><td colspan="3" style="background-color:#000; color:#FFF; font-size:16px;">Main Categories</td></tr>
<tr><td align="center" width="10%" style="font-size:14px; font-weight:bold;">ID</td>
<td align="center" style="font-size:14px; font-weight:bold;">Categorie Name</td>
<td align="center" width="30%" style="font-size:14px; font-weight:bold;">Subcategories</td></tr>
<?php
while($data = mysql_fetch_array($sql))
{
echo '<tr><input type="hidden" class="cat_id_'.$data['cat_id'].'" value="'.$data['cat_id'].'"><td align="center">'.$data['cat_id'].'</td>';
echo '<td align="center">'.ucwords(strtolower($data['cat_name'])).'</td>';
echo '<td align="center"><input type="button" class="viewData" name="'.$data['cat_id'].'" value="View" /></td></tr>';
}
?>
</table><!--</form>-->
</div>
<script type="text/javascript">
$(document).ready(function(){
$(".viewData").click(function(){
var cat_id = $(".cat_id_"+$(this).attr("name")).val();
$.post('subcategories.php',{cat_id:cat_id}, function(data){
$("#subCat").html(data);
});
return false;
});
});
</script>
<div id="subCat">
</div>
我假设你的AJAX
请求工作正常。
注:进一步为SQL查询使用准备语句。因为这个代码不安全
相关文章:
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- Javascript并不是显示在每一页上
- 如何在一页上显示多个Highcharts图表
- 一页主题搜索/用下一个按钮突出显示文本
- Angularjs:目录分页控件没有'即使按下页面按钮,也不会显示下一页
- 在表单提交后显示模态,直到加载下一页为止..不适用于狩猎
- 无法在一页上显示多个Highchart
- 计算文本字段的数量&在另一页中显示总数
- 当我们选择页面中的文本框时,在选择该chk框时在另一页中显示一些文本
- 如何在另一页上显示地图并全屏查看
- 有没有什么方法可以避免在谷歌可视化图表的图例中分页,并在一页中用两行显示所有行
- 如何在重定向到另一页之前显示正在加载的图像
- 当点击rails中的提交按钮时,如何在重定向到下一页之前显示模态
- 要显示在另一页上的表单信息
- 在iFrame中显示用户上一页
- 当用户点击链接到下一页的项目时,我如何在网页中显示加载
- jquery Mobile->页面显示,但下一页没有't
- 纵向屏幕有2页,但只能显示其中一页
- 我们可以在下一页显示response.sendRedirect发送的参数吗.如果是,如何
- 要在下一页显示的表单输入