2012-12-27 44 views
2

我正在搜索小時,但仍無法找到正確的答案。這看起來很簡單,但我需要你的幫助,這裏是 問題:有兩個按鈕,1)增加按鈕和2)遞減(減)按鈕,當我點擊按鈕1時,值$ a得到+1當我點擊按鈕2,$ a獲得-1。Onclick引導函數導致增量重置頁面,值+ 1

似乎簡單的權利?

應該回到同一頁(重裝),用更改後的值$ A

例如: $ A = 0;
1.)點擊增量按鈕 2.)重新加載頁面 $ a = 1; 3.)點擊減少按鈕 $ a = 0

非常簡單,我只是沒有那麼好自己弄清楚。

+0

你是否在增量前使用parseInt? – Anoop

+0

那麼,你想要$ a的價值繼續刷新? – swtdrgn

+0

向我們顯示您的代碼 – HBP

回答

0

我不是一個PHP開發人員,但從你的問題我明白,你失去了變量的價值,因爲它重新加載,並將所有變量重置爲其默認值。你確實要做的是堅持下去(可能在cookies,會話或服務器中,並始終從這些商店中加載價值)。

0

如果您想要保存一個要在重新加載/頁面更改後使用的變量,則需要將該變量存儲在cookie或會話中。我會在這種情況下推薦會議。所以,在這裏你有這樣一個例子:

腳本名稱:的index.php

<?PHP 
    /* You need to start a session in order to 
    * store an retrieve variables. 
    */ 
    session_start(); 
    if(!isset($_SESSION['value'])) { // If no session var exists, we create it. 
     $_SESSION['value'] = 0; // In this case, the session value start on 0. 
    } 

    if(isset($_GET['action'])) { 
     switch($_GET['action']) { 
      case 'add': // Yeah, PHP allows Strings on switchs. 
       $_SESSION['value'] ++; 
      break; 
      case 'remove': 
       $_SESSION['value'] --; 
      break; 
     } 
     /* If you avoid the next two lines, you'll be adding or removing when 
     * you refresh, so we'll redirect the user to this same page. 
     * You should change the 'index.php' for the name of your php file. 
     */ 
     header("Location: index.php"); 
     exit(); 
    } 
?> 

<html> 
    <head> 
     <title>:: Storing user values in session ::</title> 
    </head> 
    <body> 
     <p>The current value is: <?PHP echo $_SESSION['value']; ?></p> 
     <p><a href="?action=add" target="_SELF">Increase value</a></p> 
     <p><a href="?action=remove" target="_SELF">Decrease value</a></p> 
    </body> 
</html> 

如果保存代碼稱爲單一的PHP文件「的index.php」你會看到的行爲您正在尋找。

我希望這會對你有所幫助,並有一個非常快樂的新年!

PS:請注意,在這種情況下,我只用了單一的動作。我在這裏沒有使用Javascript,因爲你的問題的標題說PHP。如果你想用Javascript或JQuery做這個,請告訴我。