廖廖魔
廖廖魔
  • 发布:2015-03-24 16:59
  • 更新:2015-03-24 17:14
  • 阅读:2264

JQuery通过PHP访问数据库

分类:HTML5+

设计一个采购单管理系统。计划通过访问PHP生成一个列表,显示在HTML页面上。但是不论怎么调试,列表都无法显示出来。猜测是JS部分的问题。以为PHP部分测试是正常的。请大神帮忙看看。
HTML代码

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="utf-8" />  
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css">  
        <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>  
        <script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>  
        <script src="js/order_list.js"></script>      
        <title>订单列表</title>  
    </head>  
    <body>  
        <div data-role = "page">  
            <div data-role = "content">  
                <ul id = "orderlist" data-role = "listview" data-filter="true" >  
                    <li>  
                        <a href="#">  
                        <h2>订单1</h2>  
                        <p>采购物品:军刀;单价:10;数量:1000</p>  
                        </a></li>  
                    <li>  
                        <a href="#">  
                        <h2 id="orderID">订单</h2>  
                        <p id="order"></p>  
                        </a></li>  
                </ul>  
            </div>  
        </div>  
    </body>  
</html>

JS代码

$(function(){  
    var orderlist = $("#orderlist")  
    $.getJSON("/sql-php/order_list.php",function(json) {  
        $.each(json, function(index,array) {      
            var txt = "<li><h2>采购单"+array["order_number"]+"</h2><br/>"+  
                      "<p>采购商品:"+array["product_name"]+  
                      "<span>数量:"+array["order_quantity"]+  
                      "<span>单价:"+array["unit_price"]+"</p></li>"  
            orderlist.append(txt);   
        });  
    });  
});

PHP代码

<!DOCTYPE html>  
<html>  
    <head>  
        <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
        <title>test</title>  
    </head>  
    <body>  
        <?php  
            $con = mysqli_connect("localhost","root","","overseaorder");          
            $con -> query( 'set names gb2312');  
            $result = mysqli_query($con,"SELECT * FROM purchaseorder");  
            $rows = mysqli_num_rows($result);  
            if($result){  
                while($row = mysqli_fetch_array($result)){  
                    $orders[] = array(  
                        "_id"=>$row['_id'],  
                        "order_number"=>$row['order_number'],  
                        "product_name"=>$row['product_name'],  
                        "specification"=>$row['specification'],  
                        "note"=>$row['note'],  
                        "order_quantity"=>$row['order_quantity'],  
                        "unit_price"=>$row['unit_price(excluding_tax)'],  
                        "total"=>$row['total(excluding_tax)'],  
                        "delivery_date"=>$row['delivery_date'],  
                        "delivery_place"=>$row['delivery_place'],  
                        "quality_requirements"=>$row['quality_requirements'],  
                        "payment"=>$row['payment'],  
                        "remark"=>$row['remark'],  
                        "order_time"=>$row['order_time'],  
                        "end_time"=>$row['end_time']);  
                }  
                echo json_encode($orders);  
            }  
            else{  
                echo('Error:'.mysqli_error());  
            }  
            mysqli_close($con);  
        ?>  
    </body>  
</html>
2015-03-24 16:59 负责人:无 分享
已邀请:
太阳光

太阳光

哥,官方都说了不要使用js框架,你非用。
jQuery怎么可以能跨哉请求数据呢?直接用h5+的XMLHttpRequest吧

太阳光

太阳光

你的主要js改成:

var orderlist = $("#orderlist");  
var xhr = new plus.net.XMLHttpRequest();    
    xhr.onreadystatechange = function(){    
        if(xhr.readyState == 4 && xhr.status == 200){  
            var json = JSON.parse(xhr.responseText);  
            for(var i=0;l = json.length;i<l;i++){  
              var txt = "<li><h2>采购单"+json[i]["order_number"]+"</h2><br/>"+  
              "<p>采购商品:"+json[i]["product_name"]+  
              "<span>数量:"+json[i]["order_quantity"]+  
              "<span>单价:"+json[i]["unit_price"]+"</p></li>";              
            }  
            orderlist.append(txt);   
        }    
    };    
    xhr.open( "get", "http://www.???.com/sql-php/order_list.php");  
    //特别注意app是在你手机里,请求的是网络文件  
    xhr.send(); 

如果使用了mui你就可以使用$.getJSON( url, data, success )方法

  • 廖廖魔 (作者)

    Σ( ° △ °|||)︴我是才开始学没仔细看介绍。大哥,我按照你这样改,还是不能生成列表。而且,我发现我的PHP如果不加上(print_r($orders))放在PC浏览器测试,是不显示任何信息的。是不是我PHP输出部分有问题哦

    2015-03-24 17:35

  • 太阳光

    这个ajax可不是pc浏览器能执行的。必须是手机里执行。

    2015-03-24 17:39

  • 廖廖魔 (作者)

    回复 太阳光:大哥,我按照你的JS试了试。还是有问题。我发现我的xhr.response返回的是整个PHP页面的代码。而不是echo输出的内容。这个是为什么哦

    2015-03-25 15:17

该问题目前已经被锁定, 无法添加新回复