HTML_AJAXによるAJAXの実践(1)ーーreplaceによる画面表示変更
HTML_AJAXはPHPでAJAXを利用するためのパッケージで、
PEARライブラリを通じてインストール可能です。
2008年7月11日現在でのバージョンは0.5.6(beta版)を使っています。
root@st-desktop1:/home/WWW/HTML_AJAX/ajax# pear list Installed packages, channel pear.php.net: ========================================= Package Version State Archive_Tar 1.3.2 stable Console_Getopt 1.2.3 stable HTML_AJAX 0.5.6 beta PEAR 1.7.2 stable Structures_Graph 1.0.2 stable
この文章はHTML_AJAXを使用したAJAXの実現のサンプルを見せています。
画面作成:
<!-- page.html --> <html> <!-- HTML_AJAXを初期化 --> <script type="text/javascript" src="server.php?client=all"></script> <div id="target">I'm the target</div> <script type="text/javascript"> // 特定のページの出力で特定idのタグを更新する HTML_AJAX.replace('target','output.php'); </script> <form> <input type="button" onclick="HTML_AJAX.replace('target','output.php');" value="Update target"> </form> </html>
<?php // server.php // include 'HTML/AJAX/Server.php'; $server = new HTML_AJAX_Server(); $server->handleRequest(); ?>
バック処理
<?php // output.php // header('Cache-Control: no-cache'); header('Pragma: no-cache'); print("<h1>". strftime("%H:%M:%S") ."</h1>"); ?>
結果:
ボタンを押下する度、PHP出力により画面上に現在時刻を表示する。