php与js实现确保每次表单只能提交一次
2017-05-17 00:00
525 查看
思路:当render页面的时候,将一个随机数render给本页面,并将该值赋值给一个form表单中hiden的input框。在php设置session,初始值为空,当提交第一次时将获取到的input值赋给session;再次提交,session值就会等于之前的随机数,则不能再次提交
/** * @Route("/shangchuanfile",name="shangchuanfile") */ public function shangchuanfileAction(Request $request) { $code = mt_rand(0,1000000); $session=$request->getSession(); $session->set('getsessioncode',''); return $this->render('frontfrontBundle:filemng:shangchuanfile.html.twig',[ "getsessioncode"=>$code ]); }
/** * @Route("/uploadfile",name="uploadfile") */ public function uploadfileAction(Request $request){ $Radate=$request->request; $session=$request->getSession(); $getsessioncode=$session->get('getsessioncode'); $getcode=$Radate->get('code'); if($getsessioncode==$getcode){ } else{ if($getsessioncode==''){ $session->set('getsessioncode',$getcode); } $sqlHandle=$this->get('database_connection'); $em=$this->getDoctrine()->getManager(); $contributorid=$this->getUser()->getId(); $roles=$this->getUser()->getRoles(); $contributorname=$this->getUser()->getUsername(); $fileinfo=$_FILES['mysendFile']; $fileinfoo=explode('.',$fileinfo['name']); // $filenumM=$fileinfoo[0].date("Ymdhi"); $filenumM=uniqid(); $filenum=date("Ymdhi").(string)rand(1000,10000); $this->get("logger")->info($filenum); $this->get("logger")->info("99999999999999999"); if($fileinfo['tmp_name']!=''){ $path='images'.'/'.$contributorname; if (is_dir($path)){ $path=$path; }else{ //第3个参数“true”意思是能创建多级目录,iconv防止中文目录乱码 $res=mkdir(iconv("UTF-8", "GBK", $path),0777,true); } $ext=pathinfo($fileinfo['name'],PATHINFO_EXTENSION); $unitName=$filenumM.'.'.$ext; $destination=$path.'/'.$unitName; if(!@move_uploaded_file($fileinfo['tmp_name'],$destination)){ return new JsonResponse(array("msg"=>"文件移动失败")); } }else{ $destination='';//如果内容是写在input框,而不是上传的文件则,路径为空 } $session=$request->getSession(); $infoid=$session->get('infoID'); if($roles[0]=='ROLE_ADMIN'){ // $username=$this->getUser()->getUsername(); // $adminphone=$this->getUser()->getPhonenum(); // $adminname=$this->getUser()->getAdminName(); // $unit=uniqid(); // $addsendman=new TblContributor(); // $addsendman->setRegistdata($username) // ->setBeizgu($unit) // ->setContributorname($adminname) // ->setPhonenum($adminphone); // $em->persist($addsendman); // $em->flush(); // $contributorid=$sqlHandle->fetchAll("select ID from tbl_contributor WHERE beizgu='".$unit."' // AND registdata='".$username."' " ); $contributorid=1;//表示管理员 } $Reviewnum=$sqlHandle->fetchAll("select reviewnum from tbl_info WHERE ID='".$infoid."'"); $inter=$em->getRepository("frontfrontBundle:TblInfo")->findOneBy(array('id'=>$infoid)); $this->get('logger')->info('infooo',[$Reviewnum]); $this->get('logger')->info('infooo'); $reviewnum=$Reviewnum[0]['reviewnum']+1; $inter->setReviewnum($reviewnum); $em->persist($inter); $em->flush(); $order=new TblFile(); // $index=new TblComment(); $title=$Radate->get('filebiaoti'); $content=$Radate->get('filecontent'); $typeid=$Radate->get('filetypeid'); $order->setContrbutorid($contributorid) ->setFilebiaoti($title) ->setFileaddress($destination) ->setFilecontent($content) ->setFilenum($filenum) ->setFiletypeid($typeid) ->setIfhandout(0) ->setIfpass(0)//0表示状态是未审核 ->setIfviewpass(0)//表示未审核 ->setInfoid($infoid) ->setSenddata(date("Y-m-d h:i")); $em->persist($order); $em->flush(); // $index->setFilenum($filenum) // ->setCommentdata(date("Y/m/d h:i")); // $em->persist($index); $em->flush(); } return new JsonResponse(array("msg"=>"提交成功")); }
<input type="hidden" name="code" value="{{ getsessioncode }}"> <input type="submit" value="提交" id="tijiaoshuju" readonly="readonly" class="easyui-linkbutton" style="padding:2px 10px;width:200px;height:35px;font-size:12px;">
相关文章推荐
- php实现提交表单后保留上一次输入的值
- php 判断每人每天只能提交一次意见反馈的接口的实现
- PHP和JS实现多按钮提交表单
- flash+php实现表单提交
- iframe标签实现form表单提交无页面刷新(不需要js)---解析
- 纯javascript的ajax实现php异步提交表单
- js 回车提交表单的实现
- js实现回车提交表单
- 黄聪:Jquery+php+ajax实现表单异步提交,动态添加回复评论
- FLASH+PHP 实现表单提交
- php 解决表单重复提交实现方法介绍
- 避免表单重复提交(js实现) (转)
- PHP+JS 实现每秒刷新一次页面---小白作品
- JS实现一个表单form,多个submit按钮提交时带不同的action参数
- JQuery+PHP实现AJAX表单提交实例(推荐)(1/2)
- 避免表单重复提交(js实现) (转)
- 黄聪:Jquery+php+ajax实现表单异步提交,动态添加回复评论
- js 回车提交表单两种实现方法
- 利用js实现如果不符合规则的输入则阻止表单的提交
- 基于asp.net+ easyui框架,js提交图片,实现先上传图片再提交表单