-1

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.
After making index.html page on phonegap, I am not getting any examples or hint to connect phonegap with 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
echo $_POST['first'];
echo $_POST['second'];

?>
Mel
  • 5,837
  • 10
  • 37
  • 42
Ather ALi
  • 9
  • 1
  • 1
  • 3

3 Answers3

5

Phonegap is a mobile app development framework that uses HTML, CSS, JavaScript, and Cordova for native features. On the other hand, PHP is a server side scripting language that requires infrastructure on the server. 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.

miken32
  • 42,008
  • 16
  • 111
  • 154
Tarun
  • 101
  • 1
  • 4
1

In phonegap you can use php from your server.

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 .

and you can use normal msql connection in your server php file.

sharma sk
  • 651
  • 4
  • 14
0

I think You Did Forgot That Newer Browsers/Cordova Parsers Block Requesting To Other Websites.
My Solution For This Is Uploading The JS File Containing AJAX Trough Server And Use:
<script src="http://localhost/js/index.js"></script>

Community
  • 1
  • 1