USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART I/V)

INTRO:

Lets imagine a new project. Since it is new, you will not be ‘tied’ to dbf files,
then, you can think on ‘big things’ 🙂

The ‘big thing’ could be use a standard web server, I mean an ‘AMP’ server.

AMP stands for Apache-Mysql-Php. This is the standard technology for web servers arround the world.

If your development machine is a Windows one, you could install any AMP server there for testing.

My favorite is XAMPP: https://www.apachefriends.org/index.html

Or you could use one of the many free web services available without the need of installing anything on your machine.

THE MAIN IDEA:

This will be a sort of ‘for dummies’ explanation… here we go…

1. Our HMG (or JMG) apps will call our PHP server procedures (that resides on server).

2. The remote PHP procedures are executed doing required database work.

3. The procedures returns data (or execution result) to client app calling it.

Simple as that.

SOME SERVER CODE:

Here is a simple PHP server procedure to add a record.
We will call it ‘insert.php’:

 <?php

 $first = $_REQUEST["first"];
 $last = $_REQUEST["last"];
 
 $db = mysqli_connect( '127.0.0.1', 'root', '');

 if( ! $db ) :
 die( mysql_error() );
 endif;

 if( ! mysqli_select_db($db, 'test')):
 die( mysql_error() );
 endif;

 $sql = "INSERT INTO people (first,last) VALUES ( '".$first."','".$last."')";

 $query = mysqli_query( $db, $sql );

 if( !$query ) :
 die("error SQL");
 endif; 
 
 mysqli_close($db);

 echo 'Ok';

?>

SOME CLIENT CODE FOR WINDOWS:

Well… we will work now on our Windows client (HMG) now.

Thanks to the HBTIP Harbour contribution library, we can call a PHP server
procedure, being the server on our LAN or the Internet.

This is the trick:

// Lets assume the server are local
// and the server procedures are in the 'php' subfolder.

cServer := '127.0.0.1'
cGet := 'http://' + cServer + '/php/insert.php'


// Add parameters

cGet += '?'

cGet += 'first=' + alltrim ( InputBox('Enter Your First Name','Please...') )

cGet += '&'

cGet += 'last=' + alltrim ( InputBox('Enter Your Last Name','Thanks!') )


// Call remote server procedure with required parameters

oHttp:= TIpClientHttp():new( cGet )


// Check for error
 
IF .NOT. oHttp:open()
    MsgStop ( "Connection not available!" )
    oHttp:close()
    Return
EndIf


// Get returned data

cData := oHttp:readAll()

MSGINFO('Data returned from server: ' + cData)

oHttp:close()

SOME CLIENT CODE FOR JMG (MOBILE DEVICES):

Calling a remote procedure with JMG is very easy:

To code JMG we must use JavaScript (the programming language of our web browsers).

JavaScript is very similar to PHP, so, very simple:

function ajaxNew()
{

// We prompt user for the content of the fields for our
// new record

var new_first = prompt('Please Enter First:','');
var new_last = prompt('Please Enter Last:','');


// For simplicity, by now, we will assume that we are 
// testing on a local server 
// The PHP scripts will be stored on a sub-folder 
// called 'PHP'
// We call the insert.php procedure with the two 
// required parameters

var server = '127.0.0.1'; 
var request = $.ajax( { url: "http://" + server + "/php/insert.php" , data: { first: new_first , last: new_last } } );


// The process is ASYNCHRONOUS, meaning that our client 
// program still running
// WITHOUT WAIT for request completion.
// Then we define the required callback functions for the 
// events 'done' and 'fail'.
// One of this will be automatically executed, when our 
// request be successfully finished
// or when fail.

request.done(function()
{
    alert('New Row Added!');
});

request.fail(function(jqXHR, textStatus)
{
    alert( "Request failed: " + textStatus );
});

}

TO BE CONTINUED… (retrieve, delete, update and THE FULL WORKING CODE!)

HMG: http://www.hmgforum.com/app.php/page/download

JMG: http://www.sourceforge.net/projects/jmg

You can discuss about this post on the HMG SourceForge forum Here

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

5 Responses to USING A STANDARD WEB SERVER FOR DATA ACCESS WITH HMG AND JMG APPS (PART I/V)

  1. S. Rathinagiri says:

    Very nice Roberto. It is a real compliment to HMG.

    Like

  2. Anonymous says:

    Thanks Maestro

    Like

  3. hmgthinking says:

    Thanks to you fro reading this!

    Like

  4. esgici says:

    Thanks Maestro 😀

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s