• 利用Ajax加载数据


      

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>加载数据</title>
    <script src="bootstrap/js/jquery-1.11.2.min.js"></script>
    </head>
    
    <body>
    <h1>加载数据</h1>
    
    <select id="nation">
    </select>
    
    </body>
    <script type="text/javascript">
    //一般把这段代码写在网页最下面,但是加上这句话可将该段写在网页中任意位置
    $(document).ready(function(e) {    //ready表示当页面加载完成后
        $.ajax({
            url:"sel.php",
            dataType:"TEXT",
            success: function(data){
                var hang = data.split("|");
                var str = "";
                for(var i=0;i<hang.length;i++){
                    var lie = hang[i].split("^");
                    str = str+"<option value='"+lie[0]+"'>"+lie[1]+"</option>";
                }
                $("#nation").html(str);
            }
        });
        
    });
    </script>
    </html>

      其处理页面为:

    <?php
    require_once "DBDA.class.php";
    $db = new DBDA();
    $sql = "select * from minzu";
    $arr = $db->query($sql);
    //将数据转化成字符串
    $str = "";
    foreach($arr as $v){
        $str = $str.implode("^",$v)."|";
        }
    $str = substr($str,0,strlen($str)-1);
    echo $str;

      或可在DBDA中扩展一个方法,使其直接返回一个字符串:

    //返回字符串的方法
        public function strquery($sql,$type=0){
                $db = new MySQLi($this->host,$this->uid,$this->pwd,$this->dbname);
                $result = $db->query($sql);
                if($type){
                    return $result;
                }else{
                    $arr = $result->fetch_all();
                    $str = "";
                    foreach($arr as $v){
                        $str = $str.implode("^",$v)."|";
                        }
                    $str = substr($str,0,strlen($str)-1);
                    return $str;
            }
        }

     这样处理页面只需直接调用即可:

    <?php
    require_once "DBDA.class.php";
    $db = new DBDA();
    $sql = "select * from minzu";
    echo $db->strquery($sql);
  • 相关阅读:
    软件测试常见概念
    Apollo简介及工作原理
    bug的编写技巧与级别划分
    native与H5优缺点及H5测试
    优惠券测试
    go语言-for循环
    go语言-流程控制--if
    go语言-二进制与位运算
    cookie和session
    AJAX
  • 原文地址:https://www.cnblogs.com/maoqiaoyu123/p/8371348.html
Copyright © 2020-2023  润新知