原创文章如转载,请注明:转载自 xajax学习站
本文链接 : http://xajax.dayanmei.com/6.htm

由于表单肩负着交互的重要使命,所以处理表单就成了xajax的必修课了,对于我个人的经验,看一下本站的评论xajax是如何处理的。

由于本站使用了smarty模版,对于一些不熟悉smarty模版的朋友 增加了理解的难度,所以在这个例子中我不使用类和smarty等其他工具,只作简单阐述,力求能够理解方法

<?php

error_reporting
(E_ALL E_NOTICE);

define('ROOT_PATH',str_replace("\\","/",dirname(__FILE__)));

//载入xajax类

require_once(ROOT_PATH '/includes/xajax/xajax.inc.php');



//处理评论提交

function postcomment($form) {

     global 
$db,$tablefix;

     
$objResponse = new xajaxResponse();

     
$errormsg = array();



     
$UserName xml_escape($form['UserName']);

     
$Email xml_escape($form['Email']);

     
$Words xml_escape($form['Words']);

     
$BlogID xml_escape($form['BlogID']);



     
$IsConfirmed 0;



     
$badkey "非法关键字|非法";

     

     if(empty(
$Email)){

             
//如果没有填写电子邮件 则替换容器标签为Emailmsg内的内容为email必须填写

          
$objResponse->addAssign("Emailmsg","innerHTML","email必须填写");

          }



     if(empty(
$UserName)){

          
$errormsg['UserName'] = "姓名必须填写";

          
$objResponse->addAssign("UserNamemsg","innerHTML",$errormsg['UserName']);

          }

     

     if(empty(
$Words)){

          
$objResponse->addAssign("Wordsmsg","innerHTML","内容必须填写");

          }



     if(
preg_match("/$badkey/i",$Words)){

          
$errormsg['Words'] = "内容可能含有敏感字符";

          
$objResponse->addAssign("Wordsmsg","innerHTML",$errormsg['Words']);

          }



     if(!
is_numeric($BlogID) OR $BlogID <= 0){

          
$objResponse->addAppend("Wordsmsg","innerHTML","未知错误");

          }



     if(
count($errormsg)>0){

          
$objResponse->addAssign("addsubmit","value","发表留言");

          
$objResponse->addAssign("addsubmit","disabled",false);

          }else{

          
//没有错误则插入数据

          //这里是你处理提交进数据库

          //清空表单

          
$objResponse->addAssign("UserName","value","");

          
$objResponse->addAssign("Email","value","");

          
$objResponse->addAssign("Words","value","");

          
//添加提示信息

          
$objResponse->addAssign("addsubmit","value","如继续评论请刷新页面");

          
$objResponse->addAssign("successmsg","innerHTML","评论已经成功添加,但仍需要审核才会显示出来");

          }

          return 
$objResponse;

}



$xajax = new xajax();

//$xajax->debugOn();

$xajax->registerFunction("postcomment");

$xajax->processRequests();



?>

<!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">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>xajax留言评论处理例子</title>


<?php echo $xajax->getJavascript($CONFIG['ROOT_PATH'] . '/includes/xajax'); ?>

<script type="text/javascript">

             function sendcomment(){

                 xajax.$('addsubmit').disabled=true;

                 xajax.$('addsubmit').value="wait...";

                 xajax_postcomment(xajax.getFormValues("commentform"));

                 return false;

             }

     </script>

</head>

<body>


<h4 id="respond">发表评论</h4>

     <form name="commentform" id="commentform" onSubmit="sendcomment();" action="javascript:void(null);" method="post" enctype="multipart/form-data">

     <p><span id="successmsg" class="errormsg"></span></p>

     <p><input type="text" name="UserName" id="UserName" value="" size="22" tabindex="1" /> <label for="author"><strong>姓名</strong>(必填)</label> <span id="UserNamemsg" class="errormsg"></span></p>

     <p><input type="text" name="Email" id="Email" value="" size="22" tabindex="2" /> <label for="email"><strong>电子信箱(不公开)</strong>(必填)</label> <span id="Emailmsg" class="errormsg"></span></p>

     <p><span id="Wordsmsg" class="errormsg"></span></p>

     <p><textarea name="Words" id="Words" cols="75" rows="10" tabindex="4"></textarea></p>

     <p><input name="addsubmit" id="addsubmit" type="submit" id="submit" tabindex="5" value="提交评论" />

     <input type="hidden" name="BlogID" value="<>" />

     </p>

     </form>


</body>

</html>

上一篇:xajax中文手册 下一篇:xajax跟搜索引擎有冲突吗

时间:2007-09-29 | 分类:xajax例子 评论:阅读:4630
标签:表单,xajax

发表评论

好多网友都好踊跃,这是好的,但请不要发表无意义的评论,谢谢

最新文章 订阅xajax站最新文章

最新评论

热门文章