Ajhaxe

Asynchronous call

Now let's request a php script. Just take the same code as previous, and add the following function :

 static function asynchronousCall()
 {
      var message = "Hello World Bis !";
      var result = Http.requestUrl("scripts/script.php?message="+message);
      updateContent('HelloWorld', result);
 }
 

We have a message "Hello world Bis!" that we're going to pass to our PHP script in GET method.

var message = "Hello World Bis !";

Our http request send us back a result that we store into a variable "result".

var result = Http.requestUrl("script.php?message="+message);

We use again our function updateContent() to change our DIV's content.

updateContent('HelloWorld', result);

Just add this in your HTML page.

<div><a href="#demo" onclick="Main.asynchronousCall();">Asynchronous call</a></div>

Let's write our script in haXe targeting PHP >> Script.hx

 package;
import php.Lib;
import php.Web;
class Script 
{
	static function main()
	{
	}
	public function new()
	{
		 var params = Web.getParams();
      		 var message = params.exists('message')?params.get('message'):'No message.';
      		 Lib.print(message);
	}
	
}
 

Use Web.getParams() to get into a Hash<string> all GET and POST parameters.

If the parameter named "message" exists, we initialize the variable "message" with its value or an error message

Compile and taste it !

 haxe -js ../bin/ajhaxe.js -main Main 
 haxe -php ../bin -main Script 

Démo

(19 times)