<code><?php include "db.php"; if(isset($_POST["category"])){ $category_query = "SELECT * FROM categories"; $run_query = mysqli_query($con,$category_query); echo " <div class='nav nav-pills nav-stacked'> <li class='active'><a href='#'><h4>Categories</h4></a></li> "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; echo " <li><a href='#' class='category' cid='$cid'>$cat_name</a></li> "; } echo "</div>"; } } ?></code>
<code>//JS $(document).ready(function() { cat(); function cat() { $.ajax({ url: 'action.php', type: 'POST', data: { category: 1 } }) .done(function(data) { //console.log(data); $("#get_category").html(data); }); } })</code>
php是小白,有沒有辦法前後端分離,把echo的HTML程式碼回傳json的格式給前端處理?