Reputation: 15
I need help again... I am trying to list out the main category list, and besides each category, there is a view button to see its subs and display within the same page. Here is my code:
<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>
As you can see, I guess the jQuery function only listen to the first View button, because no matter with button I click, it only returns me the subs of the first main category. Is there any way to make it work? like using onclick action? I am not good with javascript, so I need help to get this to work...
Thank you.
Upvotes: 0
Views: 145
Reputation: 1373
Here is a working code:
<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>
I am assuming your AJAX
request works fine.
P.S. Use prepared statements for your SQL query further. Because this code is not secure.
Upvotes: 1