Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / PHP

Simulating .NET's ScriptService in PHP

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
22 Feb 2009CPOL6 min read 54.3K   392   30  
An easy-to-use class that allows us to create PHP classes whose methods are automatically exposed in client-side JavaScript.
<?
//SamplePage.php

// Clayton Rumley, digifi inc., clayton@digifi.ca
// www.digifi.ca

include_once 'digifiss.php';
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">
  <title>DigifiScriptService Sample Page</title>
  <script type="text/javascript" src="digifiss.js"></script>
  <? DigifiScriptService::add_service('MyScriptService.php'); ?>
  </head>
  <body>
  
  <a href="javascript:DoHelloWorld();">Click for Hello World</a>
  <div id="HelloWorld"></div>
  <br />
  <br />
  <a href="javascript:DoAddition();">Add 2 + 2</a>
  <div id="Addition"></div>
  <br />
  <br />
  
  <script type="text/javascript">

  function DoHelloWorld()
  {
    document.getElementById('HelloWorld').innerHTML = 'Loading...';
    MyScriptService.HelloWorld(onSuccess, onFailure, 'HelloWorld');
  }
  
  function DoAddition()
  {
    document.getElementById('Addition').innerHTML = 'Loading...';
    MyScriptService.Add(2, 2, onSuccess, onFailure, 'Addition');
  }
  
  function onSuccess(result, context, method)
  {
    document.getElementById(context).innerHTML = result;
  }
  
  function onFailure(result, context, method)
  {
    document.getElementById(context).innerHTML = result;
  }
  </script>
  </body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) digifi inc.
Canada Canada
Clayton Rumley is web developer for hire from Winnipeg, Manitoba, Canada.

Comments and Discussions