1. 程式人生 > >thinkphp 解決頁面重新整理表單重複提交的問題

thinkphp 解決頁面重新整理表單重複提交的問題

  1. 控制器中

    session_start();
    $code =mt_rand(0,1000000);
    $_SESSION['code'] = $code;
    $this->assign('code',$code);

    注:該段程式碼應在表單提交判斷的後面

    往html中傳一個session隨機數,並把該隨機數用隱藏input框的形式傳值

  2. html頁面中

    <input type="hidden" name="hiddenCode" value="<{$code}>">
  3. 在表單提交後的做判斷,若input值與session中的值相等,則可繼續提交,否則不執行提交表單

    session_start();
            if
    (isset($data['hiddenCode'])){ if($data['hiddenCode'] == $_SESSION['code']){ unset($data['hiddenCode']); //提交操作 } }
  4. 完整程式碼:

       public function test(){
            $data = I('post.');
            if($data != null){
              $this->addSubmitform($data);
            }
            session_start();
            $code
    = mt_rand(0,1000000); $_SESSION['code'] = $code; $this->assign('code',$code); $this->display('.html'); } public function addSubmitform($data){ session_start(); if(isset($data['hiddenCode'])){ if($data['hiddenCode'] == $_SESSION['code']){ unset
    ($data['hiddenCode']); $res1 = M('tableName')->add($data); if ($res1 == '') { //上傳失敗 return false; } } } }
        <form class="ticket-form" action="" method="post">
           Landing Page<input class="form-input page" type="text" name="page"/>
           <input type="hidden" name="hiddenCode" value="<{$code}>">
           <input  type="submit" value="Submit"  >
        </form>