Use ASP. NET Ajax in PHP

Source: Internet
Author: User

With the help of the open-source project PHP for Microsoft Ajax library on codeplex, we can use many core functions of ASP. NET Ajax on PHP.

 

Download and install

PHP for Microsoft Ajax library is currently only in the Alpha stage. It seems that it is a little too early to try it out.

PHP 5.2 is required in advance and the PHP-JSON module must be installed.

: Http://www.codeplex.com/phpmsajax/Release/ProjectReleases.aspx? Releaseid = 1692

Installation Method:

    1. Download and decompress PHP for Microsoft Ajax Library
    2. Download microsoft Ajax Library (http://ajax.asp.net)
    3. In the PHP Web ServiceCodeInclude msajaxservice. php.
    4. Introduce the microsoftajax. js file on the page that calls the web service.

Next, let's look at a "classic" scenario: Calling server methods to obtain complex types.

 

Write service files

Create a PHP file named employeeservice. php. First, write this sentence and include the necessary support code:

Require_once'Msajaxservice. php';

 

Then define an employee class. The four attributes are clear at a glance. Needless to say:

 
ClassEmployee
 
{
 
Public$ ID;
Public$ Name;
 
Public$ Email;
 
Public$ Salary;
 
 
 
Function _ construct ($ id, $ name, $ email, $ salary)
 
{
 
$This-> Id = $ ID;
 
$This-> Name = $ name;
 
$This-> Email = $ email;
 
$This-> Salary = $ salary;
 
}
 
}

 

The following is the employeeservice class inherited fromThe msajaxservice base class in msajaxservice. php.Define a method to return an employee object:

ClassEmployeeservice extends msajaxservice
 
{
 
Function getemployee ()
 
{
 
Return NewEmployee (12345,"Dflying Chen",Dflying@some.com", 1000 );
 
}
 
}

 

Create an employeeservice instance and call the processrequest () method of the base class to process the request:

 
$ Theservice =NewEmployeeservice ();
 
$ Theservice-> processrequest ();

 

Success!

 

Compile the call page

Create a page, PHP or HTML --ProgramRelatively simple. This time, we have no help for scriptmanager. We have to introduce the script file of the ASP. NET Ajax client and the above service. Note that employeeservice. php/JS can obtain the service client proxy, which is the same as the syntax on the ASP. NET platform:

 
<Head>
 
<Title>ASP. NET Ajax on PHP demo</Title>
 
<Script Type= "Text/JavaScript" SRC= "Microsoftajaxlibrary/microsoftajax. js"> </Script>
<SCRIPT type ="Text/JavaScript"Src ="Employeeservice. php/JS"></Script>
 
</Head>

 

The UI part of the program is very simple. The buttons are used to trigger asynchronous calls, and <DIV/> is used to display the call results:

 
<Body>
 
<Input ID= "Btngetemployee" Type= "Button" 
Value= "Get an employee" Onclick= "Return btngetemployee_onclick ()" />
 
<Div ID= "Resultdiv">
 
</Div>
 
</Body>

 

In the click event processing function of this button, the syntax for calling this service is also consistent with that in ASP. NET Ajax, which is very convenient:

 
Function btngetemployee_onclick ()
 
{
 
Employeeservice. getemployee (onsucceeded );
 
}

 

In the callback function, display the obtained employee object to resultdiv:

 
Function onsucceeded (result)
 
{
 
VaR sb =NewSYS. stringbuilder ("Server returns an employee object: <br/>");
 
SB. append ("ID :"+ Result. ID +"<Br/>");
 
SB. append ("Name :"+ Result. Name +"<Br/>");
 
SB. append ("Email :"+ Result. Email +"<Br/>");
 
SB. append ("Salary :"+ Result. Salary +"<Br/>");
 
 
$ Get ("Resultdiv"). Innerhtml = sb. tostring ();
 
}

 

Success!

 

Sample program interface

First visit

 

Click get an employee.

 

Download Sample Code

Here: aspnetajaxonphp.zip

 

I haven't used PHP for a while, and I'm a little new. I think this project is very promising. You may wish to join us. This section is very busy and I have not written any technology.ArticleThis article is also a rough article. Please forgive me.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.