您的位置:首页 > 编程语言 > PHP开发

php与js实现确保每次表单只能提交一次

2017-05-17 00:00 447 查看
思路:当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;">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: