<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的格式給前端處理?
<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的格式給前端處理?
寥寥幾行程式碼就可以分離介面與邏輯,實現MVC:
<code>/index.php (页面控制器) if(!defined('ROOT')) define('ROOT', __DIR__); require ROOT.'/include/common.php'; echo render('index.php'); //输出HTML echo json_encode(array('Server'=>'PHP')); //输出JSON /include/common.php (公共操作) if(!defined('ROOT')) exit(); require ROOT.'/include/funclass.php'; /include/funclass.php (函数和类) if(!defined('ROOT')) exit(); function render($view) { ob_end_clean(); ob_start(); require ROOT.'/view/'.$view; $html = ob_get_contents(); ob_end_clean(); ob_start(); return $html; } /view/index.php (视图) require __DIR__.'/header.php'; //if(!defined('ROOT')) exit(); require __DIR__.'/footer.php'; //JS代码一般写在footer.php里</body>前面</code>
<code>PHP中</code>
<code>echo json_encode($html);</code>
<code>前端</code>
<code>success: function(data) { $("#get_category").html(data); }</code>
php作這樣處理,單獨放到一個文件,js向這個文件請求就可以了
<code><?php include "db.php"; if(isset($_POST["category"])){ $category_query = "SELECT * FROM categories"; $run_query = mysqli_query($con,$category_query); $html=""; $html.="<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"]; $html.="<li><a href='#' class='category' cid='$cid'>$cat_name</a></li>"; } $html.="</div>"; echo $html; } } ?></code>
資料庫裡的查出來轉成數組,json_encode下輸出,js調用,取到數據,js遍歷下數組(拼接html的ajax的done裡),拼接下html,這樣php和html就完全分開了
我覺得可以先確定前端頁面想要展示哪些內容。假設這些內容有了啊,基於這些內容寫好頁面,剩下的就是這些內容對應的資料。
而php就像樓上所說的,提供一個api接口,例如返回json數據,裡面的數據就可以透過ajax來向伺服器請求。拿到資料後用js把資料填入頁裡面就好了。
我覺得你可能在想一個問題。就是輸出列表問題對吧。這個可以簡單做
ajax給前端後,前端拿到json對象,可以用前端模板引擎來做。推薦你用juicer
<code><script id="tpl" type="text/template"> <ul> {@each list as it,index} <li>${it.name} (index: ${index})</li> {@/each} {@each blah as it} <li> num: ${it.num} <br /> {@if it.num==3} {@each it.inner as it2} ${it2.time} <br /> {@/each} {@/if} </li> {@/each} </ul> </script> Javascript 代码: var data = { list: [ {name:' guokai', show: true}, {name:' benben', show: false}, {name:' dierbaby', show: true} ], blah: [ {num: 1}, {num: 2}, {num: 3, inner:[ {'time': '15:00'}, {'time': '16:00'}, {'time': '17:00'}, {'time': '18:00'} ]}, {num: 4} ] }; var tpl = document.getElementById('tpl').innerHTML; var html = juicer(tpl, data);</code>
後端:
要想回傳 json 格式: 你得把你想要回傳的 html 弄到一個陣列裡面 ,例如:
$json = array(
'html' => $html
);
echo $json;
前端接受:
$.ajax(
success: function(json){
<code>$("#get_category").html(json['html']);</code>
}
);