2014-01-29 55 views
0
頁面

我有這樣的代碼在頁面的驗證碼需要的地方:驗證碼形式提交無需重新加載

<?php 


/** Validate captcha */ 
if (!empty($_REQUEST['captcha'])) { 

    if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) !== $_SESSION['captcha']) { 
echo '<script type="text/javascript">' 
    , 'fail();' 
    , '</script>'; 
    } 
    if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) == $_SESSION['captcha']) { 
     echo '<script type="text/javascript">' 
    , 'success();' 
    , '</script>'; 
    } 

    $request_captcha = htmlspecialchars($_REQUEST['captcha']); 


    unset($_SESSION['captcha']); 


} 
if (empty($_REQUEST['captcha'])) { 
echo '<script type="text/javascript">' 
    , 'fail();' 
    , '</script>'; 
    } 

?> 

的形式如下:

<form> 
<img src="../captcha.php"/> 

<input type="text" name="captcha" /></div> 

<input name="submit"> 
</form> 

關於什麼的captcha.php我不認爲這是必要的,但如果是這樣,請讓我知道。

我需要它提交驗證碼而不刷新頁面。

我已經試過這沒有任何成功:

 $(function() { 
     $('form').bind('click', function (event) { 

event.preventDefault();// using this page stop being refreshing 

      $.ajax({ 
      type: 'POST', 
      url: '', 
      data: $('form').serialize(), 
      success: function() { 
       alert('form was submitted'); 
      } 
      }); 

     }); 
     }); 

回答

0

嘗試改變$('form').bind('click', function (event) {$('form').bind('submit', function (event) {

它幾乎沒有什麼意義綁定到窗體上的單擊事件。您要麼綁定提交按鈕上的點擊事件,要麼綁定表單上的提交事件。我推薦後者,它涵蓋了更多的案例。

+0

這有幫助,但如何檢查一切就像在PHP? – Simon

+0

我不太清楚你的意思。你能詳細說明你需要幫助的部分嗎? –

+0

3部分喜歡'if(empty($ _ SESSION ['captcha'])|| trim(strtolower($ _ REQUEST ['captcha']))!== $ _SESSION ['captcha'])巫婆檢查代碼是否輸入正確,如果沒有,一個只是提交功能(我沒有找到另一種工作方式來檢查該字段是否爲空) – Simon