2015-09-29 85 views
3

我想知道如何增加元素,每次發佈元素時。如何在提交按鈕點擊時增加值

我必須使用循環的自動增量,但我沒有得到正確的。所以任何建議或指導都會很好。

這裏是這樣,我試圖做:

感謝

<?php 

$id=0;  
if (isset($_POST['submit'])) { 
    $do = $_POST['prodCode']; 
    $di = count($do); 

    while ($di > $id) { 
    $id++; 
    echo $id; 
    } 
} 
?> 

<!DOCTYPE HTML> 
<html> 
    <head> 
    <title>Session test</title> 
    </head> 
    <body> 
    <div class="holder"> 
     <div class="im"> 
     <img src="session-test/images/bestorange-juice.jpg" /> 
     <p>bestorange-juice</p> 
     <form method="post" action="sessiontest.php"> 
      <input type="hidden" id="prodCode" name="prodCode" value="f102" /> 
      <input type="hidden" id="prodPrice" name="prodPrice" value="25" /> 
      <!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>--> 
      <input type="submit" value="send value" name="submit" id="submit" /> 
     </form> 
     </div> 
     <div class="im"> 
     <img src="session-test/images/milkshake-juice.jpg" /> 
     <p>bestorange-juice</p> 
     <form method="post" action="sessiontest.php"> 
      <input type="hidden" id="prodCode" name="prodCode" value="W122" /> 
      <input type="hidden" id="prodPrice" name="prodPrice" value="1" /> 
      <!--<input type="text" id="prodQty" name="prodQty" value="1" size="1"/>--> 
      <input type="submit" value="send value" name="submit" id="submit" /> 
     </form> 
     </div> 
    </div> 
    </body> 
</html> 
+0

你的意思增量數量被點擊,每次送超值? – mergenchik

+0

@mergenchik:是的,這就是我想增加數量..每次發送價值被點擊...謝謝 –

回答

1

是你的$_POST['prodCode']總是在形式one letter + id?如果是,也許這可能有所幫助:

if (isset($_POST['prodCode'])) { 
    $value = $_POST['prodCode']; 
    // save the letter 
    $letter = substr($value, 0, 1); 
    // get id 
    $id = (int) substr($value, 1, strlen($value) - 1); 
    // get value with letter and incremented id 
    $valueIncremented = $letter . ++$id; 
} 
// with $_POST['prodCode'] = 'f102' you will get $valueIncremented = 'f103' 

希望它有幫助。

+0

micster:沒有我的id是數字只..這個id值是僅用於測試目的.. .. –

2

嘗試下面的代碼,計數存儲在會話,但對於現實生活中的應用程序,你應該使用數據庫,你也應該讓自己的產品從數據庫:

<?php 

// initialize counts for f102 and W122 products 
if (!isset($_SESSION['count_f102']) { 
    $_SESSION['count_f102'] = 0; 
} 
if (!isset($_SESSION['count_W122']) { 
    $_SESSION['count_f102'] = 0; 
} 

if (isset($_POST['submit'])) { 
    $do = $_POST['prodCode']; 
    // increment count for product which was submitted 
    $_SESSION['count_'.$do] = 1+ (int) $_SESSION['count_'.$do]; 
} 
?> 

<!DOCTYPE HTML> 
<html> 
    <head> 
    <title>Session test</title> 
    </head> 
    <body> 
    <div class="holder"> 
     <div class="im"> 
     <img src="session-test/images/bestorange-juice.jpg" /> 
     <p>bestorange-juice</p> 
     <form method="post" action="sessiontest.php"> 
      <input type="hidden" id="prodCode" name="prodCode" value="f102" /> 
      <input type="hidden" id="prodPrice" name="prodPrice" value="25" /> 
      <input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_f102'] ?>" size="1" readonly="readonly" /> 
      <input type="submit" value="send value" name="submit" id="submit" /> 
     </form> 
     </div> 
     <div class="im"> 
     <img src="session-test/images/milkshake-juice.jpg" /> 
     <p>bestorange-juice</p> 
     <form method="post" action="sessiontest.php"> 
      <input type="hidden" id="prodCode" name="prodCode" value="W122" /> 
      <input type="hidden" id="prodPrice" name="prodPrice" value="1" /> 
      <input type="text" id="prodQty" name="prodQty" value="<?php $_SESSION['count_W122'] ?>" size="1" readonly="readonly" /> 
      <input type="submit" value="send value" name="submit" id="submit" /> 
     </form> 
     </div> 
    </div> 
    </body> 
</html>