How is javascript-php separated from HTML code?

Source: Internet
Author: User

  
   

Categories

";if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; echo "
  • $cat_name
  • "; } echo ""; }}?>
    //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);            });    }})

    PHP is small white, there is no way to separate the front and back, the echo of the HTML code back to the JSON format to the front-end processing?

    Reply content:

    
      
       

    Categories

    ";if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; echo "
  • $cat_name
  • "; } echo ""; }}?>
    //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);            });    }})

    PHP is small white, there is no way to separate the front and back, the echo of the HTML code back to the JSON format to the front-end processing?

    A few lines of code can separate the interface and logic to implement MVC:

    /index.php (页面控制器)if(!defined('ROOT')) define('ROOT', __DIR__);require ROOT.'/include/common.php';echo render('index.php'); //输出HTMLecho 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里前面

    PHP中
    echo json_encode($html);
    前端
    success: function(data) {    $("#get_category").html(data);}

    PHP for such processing, put alone in a file, JS to this file request on it can be

    
      
       

    Categories

    "; if(mysqli_num_rows($run_query)>0){ while($row = mysqli_fetch_array($run_query)){ $cid = $row["cat_id"]; $cat_name = $row["cat_title"]; $html.="
  • $cat_name
  • "; } $html.=""; echo $html; }}?>

    Database in the detection to the array, Json_encode output, JS call, take the data, JS traversal under the arrays (HTML stitching done), splicing HTML, so php and HTML are completely separated

    I think we can first determine what the front page wants to show. Let's say that this content has the AH, based on these content to write a good page, the rest is the data corresponding to the content.
    PHP, like what is said upstairs, provides an API interface, such as returning JSON data, which can be used by Ajax to request the server. After you get the data, use JS to fill in the data into the page.

    I think you might be thinking about a problem. Is the output list problem, right. This can be done simply
    Ajax to the front end, the front end to the JSON object, you can use the front-end template engine to do. Recommend you to usejuicer

    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);

    Back end:
    To return to the JSON format: you have to get the HTML you want to return in an array, for example:
    $json = Array (
    ' HTML ' = $html
    );

    Echo $json;

    Front End Acceptance:
    $.ajax (
    Success:function (JSON) {

    $("#get_category").html(json['html']);

    }
    );

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.