InlineXHR - A working example of AJAX transaction with PHP and YUI 3

You probably arrive here from a more detailed explanation of this easy to use ajax PHP framework.

On the current page we have a working example of ajax transaction. If you can't wait clicking, jump down and go ahead.

This page includes a PHP file that contains 1 PHP function that we are invoking from the JavaScript code on the present page.

The PHP function is basically what you see below (however measures are taken to remove malicious code):

 

function myPhpFunction($args,&$response){

	foreach ($args as $name =>$param){
		$params[$name] = $param;
	}
	sleep(1);	
	$response->callMethod('showResult', $params);
	$response->reply();
}

It is receiving an array of data from the JavaScript code on the page and is bouncing it back after a delay of 1 second. 

The line:

 

$response->callMethod('showResult', $params);

inside the myPhpFunction above, tells the JavaScript code to pass the params to a method of Y called showResult. If you use YUI 3 you should know what "Y" refers to.

In the HTML page we have a simple form shown below and we want to send the form content to the php function. Just edit the content as you wish










On top of that we want also to send some extra params to our function.

We do it passing a string with a series of arguments and values, say:

"username=myname&password=secret&appID=2"

 When you click on the "request server" button you send all the data to the server and you will get them back to the showResult JavaScript function, that will show them inside the console window below. Enjoy!


 

See the result

Go download it and keep me posted about how you're doing!