2013-04-15 63 views
0

我想從遠程數據庫檢索信息並將其顯示在列表中。什麼是最好的方法呢?目前我打電話給一個PHP,但無法讓它工作。任何建議?在此先感謝從PhoneGap調用PHP

---- ---- index.html的

<!DOCTYPE html> 
<html> 
<head> 
<meta name="viewport" content="width=device-width, minimum-scale=1, maximum-scale=1"> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
<title>Index</title> 
    <link href="css/custom.css" rel="stylesheet" type="text/css" /> 
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.0/jquery.mobile- 1.0.min.css" /> 
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.min.js"></script> 
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.0/jquery.mobile-1.0.min.js"></script> 
    <script src="cordova-2.6.0.js"></script> 
    <script> 
    $(document).ready(function() { 


      $("#resultBlock").html("Waiting for data..."); 

      $.get("http://localhost/select.php", {}, function(data) { 

       $("#resultBlock").html(data); 
      }) 
    }); 
    </script> 
</head> 
<body> 
<div data-role="page" id="inicio"> 
     <div data-role="header" data-position="fixed"> 
     <h1>Periódico</h1> 
     </div> 
     <div data-role="content"> 

      <p id="resultBlock"></p> 
     </div> 
     <div data-role="footer" data-id="foo1" data-position="fixed" > 
     <div data-role="navbar"> 
      <ul> 
       <li><a href="" class="ui-btn-active ui-state-persist"><img src="images/periodico-icon.png" width="40" height="40"><br/>Periodico</a></li> 
       <li><a href="#directorio" ><img src="images/directorio-icon.png" width="40" height="40"><br/>Directorio</a></li> 
       <li><a href="#clasificados" ><img src="images/clasificados-icon.png" width="40" height="40"><br/>Clasificados</a></li> 
       <li><a href="#cuponera" ><img src="images/cuponera-icon.png" width="40" height="40"><br/>Cuponera</a></li> 
      </ul> 
     </div> 
     </div> 
</div> 
</body> 
</html> 

----- ----- select.php

<?php 

$con=mysqli_connect("localhost","localhost","","test"); 
// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

$result = mysqli_query($con,"SELECT * FROM Persons"); 

echo "<table border='0'> 
<tr> 
<th>Firstname</th> 
<th>Lastname</th> 
<th>Age</th> 
</tr>"; 

while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['FirstName'] . "</td>"; 
    echo "<td>" . $row['LastName'] . "</td>"; 
    echo "<td>" . $row['Age'] . "</td>"; 
    echo "</tr>"; 
    } 

echo "</table>"; 
mysqli_close($con); 
?> 

回答

0

我記得有麻煩jquery ajax請求,同時用phonegap進行開發。嘗試使用經典的Ajax請求,它爲我工作。另一個可能的問題是您正在請求計算機上的本地文件。示例代碼如下:

function loadContent(){ 

    var xmlhttp = getXmlHttpObject();    

    xmlhttp.onreadystatechange=function() 
     {   
     if (xmlhttp.readyState==4 && xmlhttp.status==200) 
     { 
      document.getElementById("listContent").innerHTML=xmlhttp.responseText; 

     } 
     }; 
    var url = "http://example.com/select.php"; 
    xmlhttp.open("GET",url,true); 
    xmlhttp.send(); 

要獲取XMLHTTP對象:

function getXmlHttpObject(){ 
    var xmlhttp; 

if (window.XMLHttpRequest)   
    xmlhttp=new XMLHttpRequest();   
else   
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  

return xmlhttp; 

}