I am writing an Android App, and already have a working program written in HTML and PHP. Using the two, they contact an API with a user customized lookup on the html page, which then sends to the PHP page, contacts the API with the customized search, gets a result, and the php outputs to a html page again.
I know PHP cannot work on Android, but I plan on using PhoneGap. With PhoneGap, I can run JavaScript, HTML, and CSS supposedly. I have also read that a solution with Android being unable to understand PHP is to connect to a server (my computer) which can run the php for me, and then output it in a way the phone can understand.
My plan is to use JavaScript, which PhoneGap can understand, to connect to my computer, and have it run the PHP and output the page in HTML which again, PhoneGap can understand.
If this is absurd, please let me know… Otherwise I’d greatly appreciate it if someone could push me in the right direction in a JavaScript function that would allow me to authenticate myself, connect to my computer, and tell it I’d like to use a certain PHP file.
We had the exact same problem when developing our application for Android as well as for iOS. Like Austin told you already you have to make use of AJAX.
W3schools – AJAX
I recommend you however not to use jquery if it’s only needed for a few simple things because it’s fairly heavy because of the big script it has to load. So if you can reduce the amount of code, please do so by learning the real JavaScript instead of jQuery.
Also, what we did is writing our own APIRequest.js object. When calling this object like so:
This is a fairly easy approach to connect to your php which will run off course on your server somewhere in a foreign country or your pc.
As you can see we insert a functionname, we have developed our API as a fairly simple OOP php thingy that allows us to put a functionname.php in a certain folder and it will be read by de script and then select that function. Database connections and stuff like that will be aranged in the index of the api. With this approach you can make special functions, server-side, for every unique handling.
I am telling you this because you are making use of JavaScript. I’d like you to understand that it is not safe! It as as safe as a JavaScript application on your computer. It is possible for a hacker to download the .apk to his computer, run it in the simulator on his pc and make edits through his console. And thus meaning, he can change your whole code (at least, the JavaScript part). So make sure you try to make this as safe a possible, with keys and stuff like that. Also, try to do as much logic as possible on your server, so the logic can’t be changed. Only the input parameters to your API.
I hope this helped you!