簡體   English   中英

用phonegap連接php?

[英]Connect php with phonegap?

我正在使用 phonegap 制作一個應用程序,它允許 .html 表單,但我們需要 .php 格式的文件,因此我們將使用服務器進行 php。
在 phonegap 上制作 index.html 頁面后,我沒有得到任何示例或提示將 phonegap 與 php 連接。

<html>
<head>
    <style type="text/css"></style>
    <script type="text/javascript">
    function post(){
                var first = document.getElementById("first").value; 
                var second = document.getElementById("second").value;

                var formdata = new FormData();
                formdata.append("first", first);
                formdata.append("second", second);



    var ajax = new XMLHttpRequest();
    document.getElementById('ans').innerHTML = 'Loading...';
    ajax.addEventListener("load", completeHandlerrrr, false);

    ajax.open("POST", "http://localhost/phonegap/app/post.php");
    ajax.send(formdata);
}
function completeHandlerrrr(event){

var hh= event.target.responseText; 

    document.getElementById('ans').innerHTML = hh; 

}


</script>

</head>
<body>  

 <input type="text" id="first" name="first" />
 <input type="text" id="second" name="second" /> 
 <input type="submit" id="submit" name="submit" onclick="post();"/>
<div id="ans"></div>
</body>
</html>

后.php

<?php
echo $_POST['first'];
echo $_POST['second'];

?>

Phonegap 是一個移動應用程序開發框架,它使用 HTML、CSS、JavaScript 和 Cordova 來實現原生功能。 另一方面,PHP 是一種服務器端腳本語言,需要服務器上的基礎設施。 您不能直接使用 PHP,但可以通過在 JavaScript 文件中使用 Ajax 調用來使用來自遠程服務器的 PHP 頁面的結果。

在 phonegap 中,您可以使用服務器上的 php。

喜歡

ajax.open("POST", "http://192.168.1.1/phonegap/app/post.php"); // or you domain server www.example.com/api/post.php

而不是localhost

並且您可以在服務器 php 文件中使用普通的 msql 連接。

我認為您確實忘記了較新的瀏覽器/Cordova 解析器會阻止對其他網站的請求。
我的解決方案是上傳包含 AJAX 槽服務器的 JS 文件並使用:
<script src="http://localhost/js/index.js"></script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM