2014-07-23 13 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> 
相关问题