XAJAX 編程入門實例兩則

          作者:snow 日期:2006-01-25

          XAJAX是一個比較優秀的AJAX toolkit工具,安裝很簡單,就是吧他的xajax.inc.php文件copy到你制定的目錄下面就可以了。
          下面舉兩個列子來說明如何使用

          1)helloworld
          helloworld是每一個編程語言所必須的,所以這里也不例外,我會就代碼給出解釋



          <?php
          // helloworld.php demonstrates a very basic xajax implementation
          // using xajax version 0.1 beta4
          //
          http://xajax.sourceforge.n...

          require ('xajax.inc.php');? #必須的,放到腳本的最前面

          #調用函數,該函數被javascript調用
          function helloWorld($isCaps)
          {
          if ($isCaps)
          $text = "HELLO WORLD!";
          else
          $text = "Hello World!";

          $objResponse = new xajaxResponse();
          $objResponse->addAssign("div1","innerHTML",$text); #給id為div1的html元素的innerHTML屬性分配$text指,其他的方法請參考前面貼出的XAJAX介紹

          return $objResponse->getXML();?? #返回賦值后的代碼,采用xml格式
          }

          // Instantiate the xajax object. No parameters defaults requestURI to this page, method to POST, and debug to off
          $xajax = new xajax();

          //$xajax->debugOn(); // Uncomment this line to turn debugging on

          // Specify the PHP functions to wrap. The JavaScript wrappers will be named xajax_functionname
          $xajax->registerFunction("helloWorld"); #注冊函數,這個也是必須的,用來保證,javascript能調用php里面的函數
          // Process any requests. Because our requestURI is the same as our html page,
          // this must be called before any headers or HTML output have been sent
          $xajax->processRequests();
          ?>
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
          http://www.w3.org/TR/xhtm....
          <html>
          <head>
          <title>xajax example</title>
          <?php
          #輸出javascript腳本,這個方法必須放在head之間,特別是應該放在<html>標簽之前,否則無法工組
          $xajax->printJavascript();
          ?>
          </head>
          <body style="text-align:center;">
          <div id="div1" name="div1">&#160;</div>
          <br/>

          <button onclick="xajax_helloWorld(0)" >Click Me</button>
          <button onclick="xajax_helloWorld(1)" >CLICK ME</button>
          <script type="text/javascript">
          xajax_helloWorld(0); // call the helloWorld function to populate the div on load
          </script>
          </body>
          </html>


          上面的腳本是一個簡單的例子,但是體現了XAJAX使用的一個流程。需要注意的是在html腳本中,我們調用的函數名是
          xajax_helloWorld (),但是我們編程和注冊的函數名卻是helloWorld,這不是一個錯誤,而是必須這樣做,你可以查看xajax.inc.php的代碼就知道,他默 認的在html調用的函數名前綴就是xajax_helloWorld(),因此如果你不修改xajax.inc.php,那么你在html里面調用函數 就必須加上xajax_這樣的前綴。

          程序演示

          2)表單處理
          這個例子是我花時間比較多的,也是大家比較關心的,雖然官方也給出了一個form的例子,但是對表單數據的處理都是自己再重新寫的函數,而沒有使用xajax.getFormValues函數,個人覺得比較復雜。下面是官方的form演示和源代碼

          form演示

          form源代碼

          下面是我的代碼

          <?php
          require("includes/xajax.inc.php");
          function doSomething( $formData )
          {
          $objResponse = new xajaxResponse();
          $str="";
          foreach($formData as $key=>$value)
          if (empty($str))
          $str.=$key."='".$value."'";
          else
          $str.=",".$key."='".$value."'";
          //$str="form data is ".implode(",",$formData);
          //$objResponse->addAlert(print_r($formData));
          $objResponse->addAssign("result", "innerHTML", $str );
          return $objResponse->getXML();
          }

          function hb()
          {
          $objResponse = new xajaxResponse();
          $objResponse->addAssign("doIt", "value", "Working...");
          $objResponse->addAssign("doIt", "disabled", true);
          $objResponse->addScript("xajax_doSomething(xajax.getFormValues('someForm'));");

          return $objResponse->getXML();
          }


          ?>
          <?php
          $xajax = new xajax();
          //$xajax->debugOn();
          $xajax->registerFunction("doSomething");
          $xajax->registerFunction("hb");
          $xajax->processRequests();
          ?>
          <HTML>
          <HEAD>
          <?php $xajax->printJavascript(); ?>
          </HEAD>

          <BODY>
          <form id='someForm'>
          <table>
          <tr>
          <td>username:
          <input type="text" name='user' id='user' />
          </td>
          <td>password:<input type="password" id='pwd' name='pwd' /></td>
          <td><input type='button' id='doIt' value='Do It' onClick="xajax_hb()"></input>
          </td></tr>
          </table>
          </form>

          <div id='result'>
          </div>
          </BODY>
          </HTML>


          這個要注意的比較多
          a)表單的元素必須有name屬性,這個一定要注意,這是我痛苦了一天得到的結果,否則getFormValues函數將得不到表單數據。
          b) doSomething($formData)函數中的參數$formData是一個數組元素,索引是表單元素的名稱(屬性name的值),其值是表單元素的值(屬性value的值)
          c) 如果debugon打開了,可能會出現死循環的警告窗口,目前還不知道原因。
          posted on 2006-09-13 22:25 JRobot 閱讀(487) 評論(0)  編輯  收藏 所屬分類: xml相關
          主站蜘蛛池模板: 龙岩市| 香格里拉县| 鄂尔多斯市| 镇赉县| 东山县| 台南县| 泗水县| 肇庆市| 资中县| 耒阳市| 盱眙县| 社旗县| 原平市| 阿克苏市| 通许县| 大安市| 龙州县| 黑河市| 泾阳县| 抚松县| 广水市| 武安市| 兰溪市| 大港区| 临沭县| 武山县| 乌拉特中旗| 丘北县| 石林| 土默特左旗| 临清市| 武宣县| 合川市| 尼木县| 米易县| 湘乡市| 汝阳县| 沁源县| 鄂温| 六盘水市| 中西区|