xAjax的一点点使用心得

 
  1. 使用xAjax其实也没什么难的,基本上xAjax把东西都封装了一下,在使用的时候,直接拿来就OK了.下面把我做的一个关于列表显示,更改和删除的xAajx的例子拿出来瞅瞅...
  2. 先看ajax.php 这个是server端的文件,负责的是执行删除,添加,更新什么的功能.
  3. <?php
  4. session_start(); //to use the $_SESSION["mainID"] to select all the records to display.
  5. define ('XAJAX_DEFAULT_CHAR_ENCODING','UTF-8');
  6. require("top.common.php");
  7. //for run the sql query.
  8. require_once("../inc/DbObject.php");
  9. /*Insert a record. $form is a array include all the information
  10. *we get it from the dealertop...
  11. */
  12. function InsertOwner($form)
  13. {
  14.   
  15.     //tell user to wait.
  16.     $objResponse = new xajaxResponse();
  17.     $objResponse ->addAssign("ownerNotice","innerHTML","<span style='color:#ff0000'>Please wait,system is processing your request.</span>");
  18.   
  19.     //Insert Operation...
  20.     $insert = "insert into dealer_main_dealership(mainID,name,ownerShip,activeStatus) ";
  21.     $insert .= "values('".$form["mainID"]."','".$form["name"]."','".$form["owner"]."','".$form["seprateSheetA"]."')";
  22.   
  23.     $update = "update dealer_main_dealership set name='".$form["name"]."',ownerShip='".$form["owner"]."',";
  24.     $update .="activeStatus='".$form["seprateSheetA"]."' where formID='".$form["ownerFormID"]."'";
  25.     $db = new DbObject();
  26.   
  27.     //these two values get from hidden textbox.
  28.     if(!strcmp($form["ownerAction"],"insert"))
  29.     {
  30. $db->RunNonExecuteQuery($insert);
  31.    }
  32. else if(!strcmp($form["ownerAction"],"update") && strcmp($form["ownerFormID"],"0"))
  33.    { // ownerFormID should not be zero and action should be update. these two operates are setted by function SetOwnerValue()
  34.        $db->RunNonExecuteQuery($update);
  35.        $objResponse ->addAssign("ownerFormID","value""0");
  36.        $objResponse ->addAssign("ownerAction","value""insert");
  37.    }
  38. //After insert a new record we need to list this new recored.
  39.    $table = ListOwnerTable();   
  40.   
  41.    //Whatever the operation is we will set this button as default.....
  42.    $objResponse ->addAssign("btnOwner","disabled",false);
  43.    $objResponse ->addAssign("btnOwner","value","Add a item");
  44.    $objResponse ->addAssign("ownerNotice","innerHTML",$query);
  45.    $objResponse ->addAssign("ownerTable","innerHTML",$table);
  46.    return $objResponse;
  47. }
  48. function DeleteOwner($formID)
  49. {
  50. $objResponse = new xajaxResponse();
  51.     $db = new DbObject();
  52.    $db->RunNonExecuteQuery("delete from dealer_main_dealership where formID='$formID'");
  53.    //after run query we need refresh the talbe or relist all the information.
  54.     $table = ListOwnerTable();   
  55.     $objResponse ->addAssign("ownerNotice","innerHTML","<span style='color:#ff0000'>Delete completed.</span>");
  56.     $objResponse ->addAssign("ownerTable","innerHTML","$table");
  57.     return $objResponse;
  58. }
  59. //set the value to update...
  60. function SetOwnerValue($formID)
  61. {
  62. $objResponse = new xajaxResponse();
  63.    $select = "select * from dealer_main_dealership where formID=$formID";
  64.    $db = new DbObject();
  65.    $db->RunQueryCommand($select);
  66.    $row = $db->RunQueryCommand($select);
  67.    $objResponse ->addAssign("name","value",$row[0]["name"]);
  68.    $objResponse ->addAssign("owner","value"$row[0]["ownerShip"]);
  69. $objResponse ->addAssign("seprateSheetA","innerHTML","");
  70.    $objResponse ->addAssign("debug","innerHTML",$row[0]["activeStatus"]);
  71.    if(!strcmp($row[0]["activeStatus"],"Yes"))
  72.    {
  73. $objResponse ->addAssign("seprateSheetA","checked","true");
  74.        //$objResponse->addScript("alert('".$row[0]["activeStatus"]."')");
  75.     }
  76.     else if(!strcmp($row[0]["activeStatus"],"No"))
  77.     {
  78.         $objResponse ->addAssign("seprateSheetB","checked","true");
  79.     }
  80.     //Set these two value for update....
  81.     $objResponse ->addAssign("ownerFormID","value"$formID);  //important....  update will depends on this textbox
  82.     $objResponse ->addAssign("ownerAction","value""update");//important....  update will depends on this textbox
  83.     $objResponse ->addAssign("btnOwner","value","Update a item"); //set the test is suitable for this action
  84.     return $objResponse;
  85. }
  86. $xajax->processRequest();
  87. function  ListOwnerTable()
  88. {
  89.   
  90.     $db = new DbObject();
  91.     $select = "select * from dealer_main_dealership where mainID='".$_SESSION["mainID"]."'";
  92.     $rowTable = $db->RunQueryCommand($select);
  93.     if(count($rowTable)!=0)
  94.     {
  95.         //print("<p class='header2'>These are forms that you have filled in");
  96.          $table .= "<table align='center' width='100%' cellspacing='0' class='ARM_main_tablestyle2'>";
  97.          $table .="<tr>";
  98.          $table .="<th class='second'>Name</th>";
  99.          $table .="<th>%Ownership</th>";
  100.          $table .="<th>Active Y/N</th>";
  101.         $table .= "<th>Actions</th>";
  102.         $table .= "</tr>";
  103.         for($i=0;$i<count($rowTable);$i++)
  104.         {
  105.             $table .="<tr>";
  106.             $table .= "<th class='specalt2'> ".$rowTable[$i]["name"]."</th>" ;
  107.             $table .= "<th class='specalt3'> ".$rowTable[$i]["ownerShip"]."</th>";
  108.             $table .="<th class='specalt3'> "$rowTable[$i]["activeStatus"]."</th>";
  109.             $table .="<th class='specalt3'>";
  110.          
  111.            $table .="<span οnclick='xajax_SetOwnerValue(".$rowTable[$i]["formID"].")' class='edit'>Edit>></span>|";
  112.             $table .="<span οnclick='JavaScript:if(confirm(/"Are you sure?/")) {xajax_DeleteOwner(".$rowTable[$i]["formID"].")}' class='delete'>delete>></span></th>";
  113.             $table .="</tr>";
  114.         }
  115.         $table .= "</table>";
  116.     }
  117.     return $table;
  118. }
  119. ?>
  120. 然后是配置文件top.common.php,这里我叫他配置文件,就是用来注册要在后台使用到的方法的一个文件.很easy就简单的几行.
  121. <?php
  122. //this file will be requried in dealertop.php and ajax.php
  123. require_once("../../xajax/xajax.inc.php");
  124. //ajax.php is a file that include all the function.
  125. $xajax = new xajax("ajax.php");
  126. //regisister functions which will used in my program.
  127. //for insert or update it depends the hidden textbox.
  128. $xajax->registerFunction("InsertOwner"); 
  129. //Delete some record
  130. $xajax->registerFunction("DeleteOwner");
  131. //Set the value into textbox and radio button for update.
  132. $xajax->registerFunction("SetOwnerValue");
  133. ?>
  134. 最后是重头的dealertop.php了.这个文件是直接展示于前台的文件...在使用的时候最开始之前要引用配置文件require("top.common.php");
  135. echo '';
  136. 需要在Header之间引用一下,printJavascript('../../xajax/'); ?>,下面是这个文件的代码片断...有不明白的同学可以到偶的MSN来问一下.
  137.    <script type="text/javascript">
  138.        function SubmitOwner()
  139.        ...{
  140.            xajax.$('btnOwner').disabled=true;
  141.            xajax.$('btnOwner').value="please wait...";
  142.            xajax_InsertOwner(xajax.getFormValues("form1"));
  143.            return false;
  144.        }
  145.    </script>
  146. 这个是列表,在页面第一次打开的时候要展示出来的,其它时间一般都是反复的刷新这个列表..
  147.    <script type="text/javascript">
  148.        function SubmitOwner()
  149.        {
  150. <div id="ownerTable">
  151.      <?php
  152.    $select = "select * from dealer_main_dealership where mainID='".$mainID."'";
  153.    $rowTable = $db->RunQueryCommand($select);
  154.    if(count($rowTable)!=0)
  155.    {
  156. //print("<p class='header2'>These are forms that you have filled in");
  157.         $table .= "<table align='center' width='100%' cellspacing='0' class='ARM_main_tablestyle2'>";
  158.         $table .="<tr>";
  159.         $table .="<th class='second'>Name</th>";
  160.         $table .="<th>%Ownership</th>";
  161.         $table .="<th>Active Y/N</th>";
  162.        $table .= "<th>Actions</th>";
  163.        $table .= "</tr>";
  164.        for($i=0;$i<count($rowTable);$i++)
  165.         {
  166.             $table .="<tr>";
  167.            $table .= "<th class='specalt2'> ".$rowTable[$i]["name"]."</th>" ;
  168.            $table .= "<th class='specalt3'> ".$rowTable[$i]["ownerShip"]."</th>";
  169.            $table .="<th class='specalt3'> "$rowTable[$i]["activeStatus"]."</th>";
  170.            $table .="<th class='specalt3'>";
  171.           
  172. $table .="<span οnclick='xajax_SetOwnerValue(".$rowTable[$i]["formID"].")' class='edit'>Edit>></span>|";
  173.            $table .="<span οnclick='JavaScript:if(confirm(/"Are you sure?/")) {xajax_DeleteOwner(".$rowTable[$i]["formID"].")}' class='delete'>delete>></span></th>";
  174.            $table .="</tr>";
  175.        }
  176. $table .= "</table>";
  177.    }
  178. echo $table;
  179.    ?>
  180.      </div>        xajax.$('btnOwner').disabled=true;
  181.            xajax.$('btnOwner').value="please wait...";
  182.            xajax_InsertOwner(xajax.getFormValues("form1"));
  183.            return false;
  184.        }
  185. </script>
  186.  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值