discuz定义的函数showmessage()可以将弹窗以AJAX的方式弹出,首先要将模板文件中的表单按照格式编写按照格式编写.
下面以"积分转帐"页面介绍(转载请注明出处:http://www.jb51.cc/article/p-yhvsmyqn-sp.html)
模板文件
<!--{elseif $_G['gp_op'] == 'transfer'}--> <!--{if $_G[setting][transferstatus] && $_G['group']['allowtransfer']}--> <form id="transferform" name="transferform" method="post" autocomplete="off" action="home.PHP?mod=spacecp&ac=credit&op=transfer" onsubmit="ajaxpost(this.id,'return_transfercredit');"> <input type="hidden" name="formhash" value="{FORMHASH}" /> <input type="hidden" name="transfersubmit" value="true" /> <input type="hidden" name="handlekey" value="transfercredit" /> <table cellspacing="0" cellpadding="0" class="tfm mtn"> <tr> <th>{lang memcp_credits_transfer}</th> <td class="pns"> <input type="text" name="transferamount" id="transferamount" class="px" size="5" style="width: auto;" value="0" /> {$_G[setting][extcredits][$_G[setting][creditstransextra][9]][title]} {lang credits_give} <input type="text" name="to" id="to" class="px" size="15" style="width: auto;" /> </td> <td width="300" class="d"> {lang memcp_credits_transfer_min_balance} $_G[setting][transfermincredits] {$_G[setting][extcredits][$_G[setting][creditstransextra][9]][unit]}<br /> <!--{if intval($taxpercent) > 0}-->{lang credits_tax} $taxpercent<!--{/if}--> </td> </tr> <tr> <th><span class="rq">*</span>{lang transfer_login_password}</th> <td><input type="password" name="password" class="px" value="" /></td> </tr> <tr> <th>{lang credits_transfer_message}</th> <td><input type="text" name="transfermessage" class="px" size="40" /></td> </tr> <tr> <th></th> <td colspan="2"> <button type="submit" name="transfersubmit_btn" id="transfersubmit_btn" class="pn" value="true"><em>{lang memcp_credits_transfer}</em></button> <span style="display: none" id="return_transfercredit"></span> </td> </tr> </table> </form> <!--{/if}-->
看以上HTML的第4行,表单中form标签需要增加的属性及事件:(重要)
<form id="transferform" name="transferform" method="post" autocomplete="off" action="home.PHP?mod=spacecp&ac=credit&op=transfer" onsubmit="ajaxpost(this.id,'return_transfercredit');">onsubmit=ajaxpost(this.id,'return_transfercredit')是以ajaxpost方式提交,this.id是本表单id,return_transfercredit为返回信息标签ID,就是<span style="display:none" id="return_transfercredit"></span>
看以上HTML的第5,6,7行,表单中必须的3个表单项:(重要)
<input type="hidden" name="formhash" value="{FORMHASH}" /><!--discuz所有表单都有此项,机制类似于验证码,保证表单来路安全--> <input type="hidden" name="transfersubmit" value="true" /><!--为保证各个浏览器兼容性,单独添加此项,以便在后台以if(submitcheck(transfersubmit))作判断,直接给button添加name或直接给form增加name,各版本浏览器不一定发送--> <input type="hidden" name="handlekey" value="transfercredit" /><!--AJAX必须添加此项,好像是一个标识符-->
看以上HTML的第34行,ajax返回的信息将会保存到这个标签里,然后弹窗将获取此标签信息并弹出:(重要)
<span style="display: none" id="return_transfercredit"></span>
现在看一下以上3处命名规律:
onsubmit=ajaxpost(this.id,'return_transfercredit');
<input type='hidden' name='handlekey' value='transfercredit' />
<span style='display:none' id='return_transfercredit'></span>
这三个值必须保证固定格式return_xxx,xxx,return_xxx
程序文件如下:
elseif ($_G['gp_op'] == 'transfer') { if(!($_G['setting']['transferstatus'] && $_G['group']['allowtransfer'])) { showmessage('action_closed',NULL); } if(submitcheck('transfersubmit')) { if($_G['gp_to'] == $_G['username']) { showmessage('memcp_credits_transfer_msg_self_incorrect','',array(),array('showdialog' => 1,'showmsg' => true,'closetime' => true)); } $amount = intval($_G['gp_transferamount']); if($amount <= 0) { showmessage('credits_transaction_amount_invalid','closetime' => true)); } elseif(getuserprofile('extcredits'.$_G['setting']['creditstransextra'][9]) - $amount < ($minbalance = $_G['setting']['transfermincredits'])) { showmessage('credits_transfer_balance_insufficient',array('title' => $_G['setting']['extcredits'][$_G['setting']['creditstransextra'][9]]['title'],'minbalance' => $minbalance),'closetime' => true)); } elseif(!($netamount = floor($amount * (1 - $_G['setting']['creditstax'])))) { showmessage('credits_net_amount_iszero','closetime' => true)); } $to = DB::fetch_first("SELECT username,uid FROM ".DB::table('common_member')." WHERE username='$_G[gp_to]'"); if(!$to) { showmessage('memcp_credits_transfer_msg_user_incorrect','closetime' => true)); } loaducenter(); $ucresult = uc_user_login($_G['username'],$_G['gp_password']); list($tmp['uid']) = daddslashes($ucresult); if($tmp['uid'] <= 0) { showmessage('credits_password_invalid'); } updatemembercount($_G['uid'],array($_G['setting']['creditstransextra'][9] => -$amount),1,'TFR',$to['uid']); updatemembercount($to['uid'],array($_G['setting']['creditstransextra'][9] => $netamount),'RCV',$_G['uid']); if(!empty($_G['gp_transfermessage'])) { $transfermessage = dstripslashes($_G['gp_transfermessage']); notification_add($to['uid'],'credit','transfer',array('credit' => $_G['setting']['extcredits'][$_G['setting']['creditstransextra'][9]]['title'].' '.$netamount.' '.$_G['setting']['extcredits'][$_G['setting']['creditstransextra'][9]]['unit'],'transfermessage' => $transfermessage)); } showmessage('credits_transfer_succeed','home.PHP?mod=spacecp&ac=credit&op=transfer','locationtime' => true)); } }
showmessage()会返回ajax弹窗需要用到的数据,客户端会将此数据保存到<span id="retrun_xxx" style="display:none"></span>以供弹窗使用
效果如下:
上面我们说的都是ajaxpost()方法,当我们点击某个链接删除时就要使用ajaxget()
如下图:
当点击"删除"时以ajaxget()方法操作,模板如下,注意href里必须添加handlekey=xxx:
<td><a onclick="delJob(this);return false;" href="home.PHP?mod=spacecp&ac=job_company&op=employ&do=del&handlekey=deljob&jid={$val['id']}">删除</a></td>
<span id="return_deljob" style="display:none"></span>
<script type="text/javascript"> function delJob(item){ if(window.confirm("您真的要删除这条信息么?")){ var url = item.getAttribute("href"); ajaxget(url,"return_deljob"); } } </script>
程序文件如下
if(empty($arr[person])){ showmessage('人数不能为空','closetime' => true)); }
if($res){ showmessage('更新成功','home.PHP?mod=spacecp&ac=job_company&op='.$operation,'locationtime' => true)); }
这样就能完美运行AJAX弹窗.
请注意showmessage()以下面这种方式使用只会弹出窗口,此时跳转url为空,'closetime'=>true;并且自动关闭:
showmessage('The message!','closetime' => true));