You can try this. No need to do ajax.
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="index.php">Home</a>
</li>
<li class='dropdown'>
<a href="category.php" class='dropdown-toggle' data-toggle='dropdown'>
<span class='label label-pill count'></span>
<i class="glyphicon glyphicon-briefcase"></i> Category</a>
<ul class='dropdown-menu' id="category">
<?php
$query = "SELECT * FROM category";
$res = mysqli_query($link, $query);
while($row = mysqli_fetch_array($res))
{
?>
<li><a href="#"><?php echo $row["c_name"] ?></a>
<ul>
<?php
$query1 = "SELECT * FROM product where category_id =".$row['cat_id'];
$res1 = mysqli_query($link, $query1);
while($row1 = mysqli_fetch_array($res1))
{
?>
<li><a href="#"><?php echo $row1['product_name']; ?></a></li>
<?php
}
?>
</ul>
</li>';
<?php
}
?>
</ul>
</li>
<li>
<a href="contact.php">
<i class="glyphicon glyphicon-user">
</i> Contact Us</a>
</li></ul></div>
In your css hide the inner ul by default and on hover of outer ul > li > a just show the inner ul. By this you will be able to display related products to that category.
Edit
Use this in html
<ul id="nav">
<?php
$query = "SELECT * FROM category";
$res = mysqli_query($link, $query);
while($row = mysqli_fetch_array($res))
{
?>
<li><a href="#"><?php echo $row["c_name"] ?></a>
<ul>
<?php
$query1 = "SELECT * FROM product where category_id =".$row['cat_id'];
$res1 = mysqli_query($link, $query1);
while($row1 = mysqli_fetch_array($res1))
{
?>
<li><a href="#"><?php echo $row1['product_name']; ?></a></li>
<?php
}
?>
</ul>
</li>';
<?php
}
?>
<li>
<a href="contact.php">
<i class="glyphicon glyphicon-user">
</i> Contact Us</a>
</li>
</ul>
In Css
* {
margin:0;
padding:0;
}
#nav {
list-style:none;
height:2em;
}
#nav li {
position:relative;
float:left;
width:192px;
background:#999;
text-align:center;
}
#nav li:hover {
background:#777;
}
#nav a {
display:block;
color:#000;
text-decoration:none;
line-height:2em;
}
/* --------- Drop Down -------- */
#nav ul {
position:absolute;
left:-999em;
top:2em;
list-style:none;
}
#nav li:hover ul {
left:0;
top:auto;
}
Here is a JSfiddle which you can refer.
https://jsfiddle.net/SmitRaval/5t0v5nq5/
Hope this helps.