Skip to content Skip to sidebar Skip to footer

How Can I Connect To A Server Through JavaScript Where My PHP Program Is Housed?

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 th

Solution 1:

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:

var result = new APIRequest('functionname', {param1:value, param2:value})

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!


Solution 2:

Here you would need to use AJAX. jQuery has a great wrapper function called $.ajax that makes most of the process pretty simple and straightforward.

AJAX will send an asynchronous request to any file (in your case a php file) and fire a callback function with the data it receives. (synchronous is also possible, but not recommended as it will make your application hang until the request is complete. More on why this is not recommended)

Some good reads on the subjects covered here:


Solution 3:

The basic technology you want to use is AJAX, which is the term for making server calls over HTTP from Javascript. You pass data to and from the server in XML (the X in AJAX) or perhaps in another encoding, such as JSON.

You'll need a dedicated PHP file on the server that will understand the data you send in the AJAX post and instead of generating HTML generates the XML/other format your Javascript will consume.

Your best bet would probably be to create a browser app that communicates with your server via AJAX, and when that is working port it to PhoneGap.


Solution 4:

It's very easy. Just do a GET request to the PHP page and parse the result. Create a function to make it easier:

function httpGet(theUrl){
    var xmlHttp = null;
    xmlHttp = new XMLHttpRequest();
    xmlHttp.open( "GET", theUrl, false );
    xmlHttp.send( null );
    return xmlHttp.responseText;
}

Then, you can call it and obtain the resultant HTML code.

var url = 'http://yourpage/index.php?a=something&b=otherthing';
var page = httpGet(url);

Post a Comment for "How Can I Connect To A Server Through JavaScript Where My PHP Program Is Housed?"