PEAR::HTML_AJAXサンプル

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> 

<head> 
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
  <meta http-equiv="Content-Style-Type" content="text/css" /> 
  <title>HTML_AJAX Callback サンプル</title>
  <script type="text/javascript" src="js/prototype.js"></script>
  <script type="text/javascript" src="ajax_server1.php?client=all"></script>
  <script type="text/javascript" src="ajax_server1.php?stub=test"></script>
  <script type="text/javascript" src="validation.php?client=all"></script>
  <script type="text/javascript" src="validation.php?stub=validation"></script>

<script type="text/javascript">

  /* Ajax Server1 */

  function callback_test() {}
  callback_test.prototype = {
      say_hello: function(resp) {
        document.getElementById('result').innerHTML = resp;
      }
  }

  var proxy = new test(new callback_test());

  /* Validation */

  function callback_validation() {}
  callback_validation.prototype = {
      checkNum: function(resp) {
        document.getElementById('result_validation').innerHTML = resp;
      }
  }

  var proxy_validation = new validation(new callback_validation());
</script> 
</head> 

<body> 
<label for="num">入力してください:</label> 
<input type="text" name="num" id="num" value="" onKeyUp="javascript:proxy_validation.checkNum($F('num'));" /> 
*onKeyUpイベントに反応



<a href="javascript:proxy.say_hello($F('num'));">Run message</a>  ⇒

message: <span id="result" style="background:#FFAAAE"></span>


 
<a href="javascript:proxy_validation.checkNum($F('num'));">Run validation </a>  ⇒

validation: <span id="result_validation" style="background:#FFDBAA"></span> 


</body> 
</html> 

[PHP] サーバー側(1)

<?php 
require  'HTML/AJAX/Server.php';
/**
 * Test Class
 * 
 */

class test {
  function say_hello($name) {
    return "Hello $name !";
  }
}
$test = new HTML_AJAX_Server();
$test->registerClass(new test());
$test->handleRequest();
?> 
**[PHP] サーバー側(2)
<?php 
//-----------------------validation.php

require  'HTML/AJAX/Server.php';
class validation{ 
  function validation(){ 
  } 
  function checkNum($num){ 
    $escape_num = htmlspecialchars($num, ENT_QUOTES); 
    if (strlen($escape_num) == 0) { 
      //exit; 
      return '未入力・空です'; 
    } 
    if (preg_match("/^[0-9]+$/", $escape_num)) { 
      return '半角数字です'; 
    } else { 
      return '半角数字ではありません'; 
    } 
  } 
}

$validation = new HTML_AJAX_Server();
$validation->registerClass(new validation());
$validation->handleRequest();
 
?>