Geeks With Blogs


Timmy Kokke's Blog

↑ Grab this Headline Animator

Timmy Kokke at Blogged
Timmy Kokke …just sorting my bubbles…


For my entry for the WinPHP challenge, my personal Blog Engine “BlogSnor”, I wanted to make use of Ajax to retrieve posts, comments, color data and more from the server. I’ve used Microsoft Ajax in ASP.NET before, so why not try that in PHP too. Here’s how…



For the simplicity of this tutorial I make the assumption you have php 5.2 or higher running in iis7 and that your localhost points to c:/wwwroot/inetpub. (If you have not, and you have questions about this, just send me an email and I’ll try to help you out).

Let’s start by creating two directories. The first one will the root of the tutorial. Create a directory in c:\wwwroot\inetpub and call it MsAjaxTutorial. Create a subdirectory in MsAjaxTutorial and call it MsAjax.

Next, download the PHP for Microsoft AJAX Library from CodePlex and unzip to MsAjax. This will create the intermediate layer between PHP and Microsoft Ajax. The extraction of the zip file create the MicrosoftAjaxLibrary directory in which the Microsoft Ajax Library will come. Download the Microsoft AJAX Library 3.5 and unzip this into MsAjax also.


The Service

Create a php file that will be the service that is called. In this tutorial I’ve named this TutorialService.php. The service will validate if a number is between 1 and 10 and returns a response based on the result of that. Add the following code to the file:

require_once 'MsAjax/dist/MSAjaxService.php'; 
class TutorialService extends MSAjaxService 
    function Validate($number) 
        if($number>=1 && $number <=10) 
            return "<font color='darkgreen'>number is valid</font>"; 
            return "<font color='red'>number is not between 1 and 10</font>"; 
$h = new TutorialService(); 

First  the MSAjaxService.php file is included. This provides the MsAjaxService class which enables all functionality needed to use Microsoft Ajax.

The TutorialService class inherits MsAjaxService, so we can extend the service with our own functions.

The Validate($number) function is pretty self explaining. It just checks if a number is between 1 and 10 and respond to that.

The last two lines are necessary for creating an instance of the TutorialService and creating the proxy to call from the client.


The Client

The client is a simple html file in this case. First, have a look at the code.

    <script type="text/javascript" src =  
    <script type="text/javascript" src="TutorialService.php/js"></script> 
    enter a number between 1 and 10: <input id="number" type="text" onblur="validate()"/> 
    <div id="response"></div> 
  <script type="text/javascript">
    function validate() { 
              function (result) { 
                $get('response').innerHTML = result; 



The client needs javascript a reference to the MicrosoftAjax javascript library and a reference to the TutorialService. Note the “ /js “ parameter in the path to the source there. This parameter is picked up by the ProcessRequest method on the server and makes sure the necessary javascript is returned.

The body contains a text and an textbox. The textbox will be validated when it’s focus is lost. To test it, just enter a number and press tab. 

The onblur event of the textbox calls the validate() method specified in the last part of the html file. This method calls TutorialService.Validate() method passing the number entered in the textbox and a callback function. The callback functions changes the innerHtml of the <div> with id response to  the result from the server.


That’s it. No complicated calls to objects, just two references and a simple function.

Shout it

Posted on Tuesday, May 19, 2009 11:40 AM WinPHP , PHP , Microsoft Ajax | Back to top

Comments on this post: Using Microsoft Ajax in PHP

# re: Using Microsoft Ajax in PHP
Requesting Gravatar...
excellent i used in my page thanks
Left by Miguel on Feb 26, 2012 7:18 PM

Your comment:
 (will show your gravatar)

Copyright © Timmy Kokke | Powered by: