简体   繁体   English

用phonegap连接php?

[英]Connect php with phonegap?

I am making an app using phonegap and it allows .html form but we require .php format files so from will we use a server for php.我正在使用 phonegap 制作一个应用程序,它允许 .html 表单,但我们需要 .php 格式的文件,因此我们将使用服务器进行 php。
After making index.html page on phonegap, I am not getting any examples or hint to connect phonegap with 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>

post.php后.php

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

?>

Phonegap is a mobile app development framework that uses HTML, CSS, JavaScript, and Cordova for native features. Phonegap 是一个移动应用程序开发框架,它使用 HTML、CSS、JavaScript 和 Cordova 来实现原生功能。 On the other hand, PHP is a server side scripting language that requires infrastructure on the server.另一方面,PHP 是一种服务器端脚本语言,需要服务器上的基础设施。 You can't use PHP directly, but you can use the results of a PHP page from a remote server by using an Ajax call in your JavaScript file.您不能直接使用 PHP,但可以通过在 JavaScript 文件中使用 Ajax 调用来使用来自远程服务器的 PHP 页面的结果。

In phonegap you can use php from your server.在 phonegap 中,您可以使用服务器上的 php。

like喜欢

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

instead for localhost .而不是localhost

and you can use normal msql connection in your server php file.并且您可以在服务器 php 文件中使用普通的 msql 连接。

I think You Did Forgot That Newer Browsers/Cordova Parsers Block Requesting To Other Websites.我认为您确实忘记了较新的浏览器/Cordova 解析器会阻止对其他网站的请求。
My Solution For This Is Uploading The JS File Containing AJAX Trough Server And Use:我的解决方案是上传包含 AJAX 槽服务器的 JS 文件并使用:
<script src="http://localhost/js/index.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM