- 使用xAjax其实也没什么难的,基本上xAjax把东西都封装了一下,在使用的时候,直接拿来就OK了.下面把我做的一个关于列表显示,更改和删除的xAajx的例子拿出来瞅瞅...
- 先看ajax.php 这个是server端的文件,负责的是执行删除,添加,更新什么的功能.
- <?php
- session_start(); //to use the $_SESSION["mainID"] to select all the records to display.
- define ('XAJAX_DEFAULT_CHAR_ENCODING','UTF-8');
- require("top.common.php");
- //for run the sql query.
- require_once("../inc/DbObject.php");
- /*Insert a record. $form is a array include all the information
- *we get it from the dealertop...
- */
- function InsertOwner($form)
- {
- //tell user to wait.
- $objResponse = new xajaxResponse();
- $objResponse ->addAssign("ownerNotice","innerHTML","<span style='color:#ff0000'>Please wait,system is processing your request.</span>");
- //Insert Operation...
- $insert = "insert into dealer_main_dealership(mainID,name,ownerShip,activeStatus) ";
- $insert .= "values('".$form["mainID"]."','".$form["name"]."','".$form["owner"]."','".$form["seprateSheetA"]."')";
- $update = "update dealer_main_dealership set name='".$form["name"]."',ownerShip='".$form["owner"]."',";
- $update .="activeStatus='".$form["seprateSheetA"]."' where formID='".$form["ownerFormID"]."'";
- $db = new DbObject();
- //these two values get from hidden textbox.
- if(!strcmp($form["ownerAction"],"insert"))
- {
- $db->RunNonExecuteQuery($insert);
- }
- else if(!strcmp($form["ownerAction"],"update") && strcmp($form["ownerFormID"],"0"))
- { // ownerFormID should not be zero and action should be update. these two operates are setted by function SetOwnerValue()
- $db->RunNonExecuteQuery($update);
- $objResponse ->addAssign("ownerFormID","value", "0");
- $objResponse ->addAssign("ownerAction","value", "insert");
- }
- //After insert a new record we need to list this new recored.
- $table = ListOwnerTable();
- //Whatever the operation is we will set this button as default.....
- $objResponse ->addAssign("btnOwner","disabled",false);
- $objResponse ->addAssign("btnOwner","value","Add a item");
- $objResponse ->addAssign("ownerNotice","innerHTML",$query);
- $objResponse ->addAssign("ownerTable","innerHTML",$table);
- return $objResponse;
- }
- function DeleteOwner($formID)
- {
- $objResponse = new xajaxResponse();
- $db = new DbObject();
- $db->RunNonExecuteQuery("delete from dealer_main_dealership where formID='$formID'");
- //after run query we need refresh the talbe or relist all the information.
- $table = ListOwnerTable();
- $objResponse ->addAssign("ownerNotice","innerHTML","<span style='color:#ff0000'>Delete completed.</span>");
- $objResponse ->addAssign("ownerTable","innerHTML","$table");
- return $objResponse;
- }
- //set the value to update...
- function SetOwnerValue($formID)
- {
- $objResponse = new xajaxResponse();
- $select = "select * from dealer_main_dealership where formID=$formID";
- $db = new DbObject();
- $db->RunQueryCommand($select);
- $row = $db->RunQueryCommand($select);
- $objResponse ->addAssign("name","value",$row[0]["name"]);
- $objResponse ->addAssign("owner","value", $row[0]["ownerShip"]);
- $objResponse ->addAssign("seprateSheetA","innerHTML","");
- $objResponse ->addAssign("debug","innerHTML",$row[0]["activeStatus"]);
- if(!strcmp($row[0]["activeStatus"],"Yes"))
- {
- $objResponse ->addAssign("seprateSheetA","checked","true");
- //$objResponse->addScript("alert('".$row[0]["activeStatus"]."')");
- }
- else if(!strcmp($row[0]["activeStatus"],"No"))
- {
- $objResponse ->addAssign("seprateSheetB","checked","true");
- }
- //Set these two value for update....
- $objResponse ->addAssign("ownerFormID","value", $formID); //important.... update will depends on this textbox
- $objResponse ->addAssign("ownerAction","value", "update");//important.... update will depends on this textbox
- $objResponse ->addAssign("btnOwner","value","Update a item"); //set the test is suitable for this action
- return $objResponse;
- }
- $xajax->processRequest();
- function ListOwnerTable()
- {
- $db = new DbObject();
- $select = "select * from dealer_main_dealership where mainID='".$_SESSION["mainID"]."'";
- $rowTable = $db->RunQueryCommand($select);
- if(count($rowTable)!=0)
- {
- //print("<p class='header2'>These are forms that you have filled in");
- $table .= "<table align='center' width='100%' cellspacing='0' class='ARM_main_tablestyle2'>";
- $table .="<tr>";
- $table .="<th class='second'>Name</th>";
- $table .="<th>%Ownership</th>";
- $table .="<th>Active Y/N</th>";
- $table .= "<th>Actions</th>";
- $table .= "</tr>";
- for($i=0;$i<count($rowTable);$i++)
- {
- $table .="<tr>";
- $table .= "<th class='specalt2'> ".$rowTable[$i]["name"]."</th>" ;
- $table .= "<th class='specalt3'> ".$rowTable[$i]["ownerShip"]."</th>";
- $table .="<th class='specalt3'> ". $rowTable[$i]["activeStatus"]."</th>";
- $table .="<th class='specalt3'>";
- $table .="<span οnclick='xajax_SetOwnerValue(".$rowTable[$i]["formID"].")' class='edit'>Edit>></span>|";
- $table .="<span οnclick='JavaScript:if(confirm(/"Are you sure?/")) {xajax_DeleteOwner(".$rowTable[$i]["formID"].")}' class='delete'>delete>></span></th>";
- $table .="</tr>";
- }
- $table .= "</table>";
- }
- return $table;
- }
- ?>
- 然后是配置文件top.common.php,这里我叫他配置文件,就是用来注册要在后台使用到的方法的一个文件.很easy就简单的几行.
- <?php
- //this file will be requried in dealertop.php and ajax.php
- require_once("../../xajax/xajax.inc.php");
- //ajax.php is a file that include all the function.
- $xajax = new xajax("ajax.php");
- //regisister functions which will used in my program.
- //for insert or update it depends the hidden textbox.
- $xajax->registerFunction("InsertOwner");
- //Delete some record
- $xajax->registerFunction("DeleteOwner");
- //Set the value into textbox and radio button for update.
- $xajax->registerFunction("SetOwnerValue");
- ?>
- 最后是重头的dealertop.php了.这个文件是直接展示于前台的文件...在使用的时候最开始之前要引用配置文件require("top.common.php");
- echo '';
- 需要在Header之间引用一下,printJavascript('../../xajax/'); ?>,下面是这个文件的代码片断...有不明白的同学可以到偶的MSN来问一下.
- <script type="text/javascript">
- function SubmitOwner()
- ...{
- xajax.$('btnOwner').disabled=true;
- xajax.$('btnOwner').value="please wait...";
- xajax_InsertOwner(xajax.getFormValues("form1"));
- return false;
- }
- </script>
- 这个是列表,在页面第一次打开的时候要展示出来的,其它时间一般都是反复的刷新这个列表..
- <script type="text/javascript">
- function SubmitOwner()
- {
- <div id="ownerTable">
- <?php
- $select = "select * from dealer_main_dealership where mainID='".$mainID."'";
- $rowTable = $db->RunQueryCommand($select);
- if(count($rowTable)!=0)
- {
- //print("<p class='header2'>These are forms that you have filled in");
- $table .= "<table align='center' width='100%' cellspacing='0' class='ARM_main_tablestyle2'>";
- $table .="<tr>";
- $table .="<th class='second'>Name</th>";
- $table .="<th>%Ownership</th>";
- $table .="<th>Active Y/N</th>";
- $table .= "<th>Actions</th>";
- $table .= "</tr>";
- for($i=0;$i<count($rowTable);$i++)
- {
- $table .="<tr>";
- $table .= "<th class='specalt2'> ".$rowTable[$i]["name"]."</th>" ;
- $table .= "<th class='specalt3'> ".$rowTable[$i]["ownerShip"]."</th>";
- $table .="<th class='specalt3'> ". $rowTable[$i]["activeStatus"]."</th>";
- $table .="<th class='specalt3'>";
- $table .="<span οnclick='xajax_SetOwnerValue(".$rowTable[$i]["formID"].")' class='edit'>Edit>></span>|";
- $table .="<span οnclick='JavaScript:if(confirm(/"Are you sure?/")) {xajax_DeleteOwner(".$rowTable[$i]["formID"].")}' class='delete'>delete>></span></th>";
- $table .="</tr>";
- }
- $table .= "</table>";
- }
- echo $table;
- ?>
- </div> xajax.$('btnOwner').disabled=true;
- xajax.$('btnOwner').value="please wait...";
- xajax_InsertOwner(xajax.getFormValues("form1"));
- return false;
- }
- </script>
xAjax的一点点使用心得
最新推荐文章于 2021-03-21 08:43:32 发布