2014-07-23 14 views
0

我是Cordova應用程序中的新成員。我想問你如何使用php和MongoDB在cordova應用程序中從表單發佈數據。我在c:/ xampp/htdocs中的cordova app和comment.php中有index.html。我想在index.html的comment.php中顯示數據。這裏的代碼。這是index.html,然後comment.php如何使用php和MongoDB發佈Cordova應用程序中的數據

的index.html

<!DOCTYPE html> 
<html> 
    <head> 
     <title>jQM Complex Demo</title> 
     <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/> 
     <meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi"/> 
     <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" /> 
     <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
     <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script> 
     <script> 
      $.ajax({ 
       type : "POST", 
       url : "comment.php", 
       crossDomain: true, 
       beforeSend : function() {$.mobile.loading('show')}, 
       complete : function() {$.mobile.loading('hide')}, 
       data : {email : 'email', comment : 'comment'}, 
       dataType : 'json', 
       success : function(response) { 
        //console.error(JSON.stringify(response)); 
        alert('Works!'); 
       }, 
       error : function() { 
        //console.error("error"); 
        alert('Not working!'); 
       } 
      }); 
     </script> 
    </head> 
    <body> 
     <script> 
      document.addEventListener("deviceready", function() { 
       new kendo.mobile.Application(document.body, { 
        statusBarStyle: "black-translucent" 
       }); 
      }, false); 
     </script> 
     <div data-role="page" id="index"> 
      <div data-theme="b" data-role="header"> 
       <h1>Index page</h1> 
      </div> 
      <div data-role="content"> 
      </div> 
     </div> 
    </body> 
</html> 

comment.php

$email = isset($_POST['email']) ? $_POST['email'] : ''; 
    echo $email; 
    $comment = isset($_POST['comment']) ? $_POST['comment'] : ''; 
    echo $comment; 

回答

0

科爾多瓦不支持PHP僅文件HTMLJavascript。所以如果你想以mongodb作爲你的默認數據庫,你需要使用一個REST Service(在JavaScript中),它指向一個在線api(也許是php),它可以處理你的mongodb的數據請求(安裝在在線服務器上)並提供信息到您的應用程序。

如果你喜歡數據庫集成到你的應用程序,那麼你可以使用SQLlite數據庫,並在這裏看看。

https://github.com/brodysoft/Cordova-SQLitePlugin

0

在命令行檢查IP

IPCONFIG

Ethernet adapter Local Area Connection: 

    Connection-specific DNS Suffix . : 
    Link-local IPv6 Address . . . . . : fe80::19a7:4cc9:c1e8:f9ef%11 
    IPv4 Address. . . . . . . . . . . : 192..168.one.three 
    Subnet Mask . . . . . . . . . . . : 255 .255 .254 .0 
    Default Gateway . . . . . . . . . : 192 .168 .1 .1 

現在你的服務器IP地址將被192.168.1.3

你可以訪問你c:/xampp/htdocs/comments.php使用http://192.168.one.three/comments.php內phonegap應用程序。但如果電腦和手機在同一個網絡中。

Phonegap本身在android應用程序(apk)內創建一個服務器(localhost)。

注:192..168.one.three === 192.168.1.3

<script> 
      $.ajax({ 
       type : "POST", 
       url : "http://192.168.1.3/comment.php", 
       crossDomain: true, 
       beforeSend : function() {$.mobile.loading('show')}, 
       complete : function() {$.mobile.loading('hide')}, 
       data : {email : 'email', comment : 'comment'}, 
       dataType : 'json', 
       success : function(response) { 
        //console.error(JSON.stringify(response)); 
        alert('Works!'); 
       }, 
       error : function() { 
        //console.error("error"); 
        alert('Not working!'); 
       } 
      }); 
     </script> 
相關問題