2013-05-07 14 views
0

我有2個按鈕。jQuery或PHP(我該怎麼辦)提交前必須等待8秒

按鈕A:一個按鈕。 按鈕B:提交按鈕。

我想要做的是,當用戶點擊按鈕A時,它會發送給他一個鏈接。 但是他將無法使用按鈕B提交,直到他點擊按鈕A以來的8秒。

我不確定它是否可以使用PHP,是嗎? 如果不是,我將如何使用jQuery來做到這一點?

也許設置一個日期點擊使用jQuery像var futureTime = time + 8sec。如果(!futureTime){呼應這個}

+0

一些JavaScript可以在客戶端執行結束它,在該窗口/ TAB。但用戶可以簡單地提取鏈接並在另一個窗口中使用它,並完全繞過限制,因此您還必須在服務器上強制執行該限制。 – 2013-05-07 21:49:28

+0

你可以禁用按鈕B,當按鈕一被點擊,並在X時間後重新啓用它..你可以使用jQuery和CSS來實現這個例如:$(「input [type = submit]」)。attr(「禁用「,」禁用「); – Dinesh 2013-05-07 21:49:35

+0

如果用戶禁用js? – 2013-05-07 21:49:41

回答

0

你應該能夠做這樣的事情:

$("button#a").click(function(){ 
    $("button#b").prop('disabled', true); 
    setTimeout(function(){ 
     $("button#b").prop('disabled', false); 
    }, 8000); 
}); 

如果通過發送一個鏈接,你的意思是「翻開新的一頁」,那麼你可以只添加這到被打開的網頁上您的負載腳本:

$("button#b").prop('disabled', true); 
    setTimeout(function(){ 
     $("button#b").prop('disabled', false); 
    }, 8000); 
+0

如果我想發送用戶如果他在8秒之前點擊提交失敗,那麼錯誤? – 2013-05-07 21:52:19

+0

然後你不能使用「禁用」,並需要用不同的邏輯處理單擊事件:按鈕#a - >設置一些布爾爲假 - >點擊#B - >如果布爾是假顯示錯誤,否則請提交 – fmsf 2013-05-07 21:55:44

+0

您能舉個例子嗎?我剛剛嘗試過這樣做,JS說我的var沒有定義,但如果我將它定義爲false,js會覆蓋true到false? – 2013-05-07 22:02:39

0

正如在評論中指出,應檢查使用PHP的形式,如JavaScript可以將其關閉。特別是在處理敏感數據時,人們會嘗試通過JavaScript部分。

這裏有一個想法,您可以使用它來檢查用戶在表單中發送的時間。如果「太早」出現,您可以創建一個錯誤,否則您可以執行您的邏輯。您應該將其與JavaScript建議組合,如fmsf,以避免大量發送表單。

PHP

// start the session to store the current time and a key 
session_start(); 

// check if the key exists in the session and was the send with the form 
if (isset($_SESSION['key']) && 
    isset($_POST['key']) && 
    $_SESSION['key'] == $_POST['key']) { 

    // if the difference to "now" is more than 8 seconds include your logic 
    if (8 < time() - $_SESSION['timestamp']) { 
     // your logic 
    } else { 
     // create an error message 
    } 
} 

// create a new key and store it together with the current timestamp 
$key = uniqid('', true); 
$_SESSION['key'] = $key; 
$_SESSION['timestamp'] = time(); 

HTML

<form action="" method="post"> 
    <fieldset> 
     <!-- include the key in a hidden field in the form --> 
     <input type="hidden" name="key" value="<?php print $key; ?>"> 
     <input type="text" name="value" value=""> 
     <input type="submit" name="submit" value="submit"> 
    </fieldset> 
</form> 
相關問題