2014-09-02 45 views
0

我有一個問題,我正在使用一個按鈕上的網站。 問題是我只希望這個訪問該網站的用戶按下一次。 因此,當用戶刷新或回來時,不可能再次按下它。即使在下次訪問時點擊使按鈕消失

我想我應該使用PHP與AJAX的組合,但不幸的是我不夠好,不能將它放在一起。在這裏,希望有人可以把我在正確的方向:)

html代碼:

<form action="" method="POST"> 

    <input id="banana_button" type="submit" name="button_press" value="Add banana's!"> 
</form> 

的JavaScript:

$('input[name="button_press"').click(function(){ 

     $('#banana_counter_text').append($banana+1); 
     $(this).attr('disabled',true).css('width','180px').attr('value','thanks for adding a banana'); 
    }); 

我想要得到的是,當他們按下了按鈕下一次他們訪問他們將看不到按鈕。 是否有可能,我怎麼能最好這樣做。我希望你們能幫助我

更新:

嗯,我想最近幾天來解決這個問題,我得到它的工作。 我正在使用具有自定義名稱的session_cookie。每次網站加載時,它都會檢查該cookie是否可用,如果不是,則會顯示該按鈕。否則它會刪除整個按鈕。

的代碼如下所示:

PHP

//this is what checks everytime if the cookie is set 
    if (isset($_COOKIE['banana_added'])) { 
     //code for disabling the button 
    } 

//this is what makes the cookie and checks if the submit button is pressed. 
//because people where wondering how the data is stored. It is just a simple XML file 
    if(isset($_POST['buttonsubmit'])){ 
     /*when submit is pressed */ 
     session_name("banana_added"); 
     session_start(); 
     $xml = simplexml_load_file('../banana.xml'); 
     $prev_count = $xml->banana_count; 
     $new_count = $prev_count +$_POST['buttonsubmit']; 
     echo $prev_count; 
     echo $new_count; 
     $xml->banana_count = $new_count; 
     file_put_contents('../banana.xml', $xml->asXML()); 
     header("location:../landing.php"); 

感謝大家的幫助。我不知道如何關閉此線程? 這是這個特殊問題

+0

是用戶只有在他們登錄時才能點擊或者這樣? – Class 2014-09-02 22:32:20

+0

您將PHP作爲標記之一。您是否使用PHP來管理您的數據(例如,將其存儲在數據庫中)?您正在尋找的功能類型是「一次性」功能,一次出現,然後一旦用戶提供輸入就會禁用它。 – kevin628 2014-09-02 22:32:28

+0

簡單的解決方案是存儲cookie並在cookie存在時隱藏該按鈕。這並不妨礙人們「清除cookie緩存」並重新提交。爲了更明確的控制,你需要一個服務器端解決方案,這會帶來很多複雜的識別和存儲'已訪問'的用戶。 – 2014-09-02 22:36:33

回答

0

爲了記住,改變你要麼把該值COOKIE /會話或數據庫中的解決方案。課程的第二個變體是偏好的。 所以讓例如說,你有臺名爲按鈕,並在其中你有一列「檢查」類型TINYINT(接受值0和1),默認爲0

值在HTML,你應該這樣做:

<button <?php ($thisButtonQuery->checked == 'checked')? 'disabled' : '';?>>button text </button> 

關心,我希望我解決了你的問題! :)

+0

我從來沒有使用過數據庫,因此我很難設置它。有沒有可能通過使用cookie等解釋的方法? – 2014-09-02 23:39:01

0

嗯,我正在試圖解決這個問題的最後幾天,我得到它的工作。我正在使用具有自定義名稱的session_cookie。每次網站加載時,它都會檢查該cookie是否可用,如果不是,則會顯示該按鈕。否則它會刪除整個按鈕。

的代碼如下所示:

PHP 

//this is what checks everytime if the cookie is set 
    if (isset($_COOKIE['banana_added'])) { 
     //code for disabling the button 
    } 

//this is what makes the cookie and checks if the submit button is pressed. 
//because people where wondering how the data is stored. It is just a simple XML file 
    if(isset($_POST['buttonsubmit'])){ 
     /*when submit is pressed */ 
     session_name("banana_added"); 
     session_start(); 
     $xml = simplexml_load_file('../banana.xml'); 
     $prev_count = $xml->banana_count; 
     $new_count = $prev_count +$_POST['buttonsubmit']; 
     echo $prev_count; 
     echo $new_count; 
     $xml->banana_count = $new_count; 
     file_put_contents('../banana.xml', $xml->asXML()); 
     header("location:../landing.php"); 

感謝大家的幫助。 這是針對這個特定問題的解決方案