2015-10-19 58 views
0

我試圖用在文本框中輸入的用戶請求更新表格。 因此,如果用戶想要有5行輸入訂單表格中的數據,他們會在文本框中輸入'5',點擊更新按鈕,它將用5行填充表格。 我相信我的代碼中有邏輯關係,但由於某種原因它沒有更新。任何想法爲什麼?如何通過點擊按鈕添加更多行來更新表格

<table> 
<tr> 
    <th class="textCol">Product Rows:</th> 
    <td class="inputCol"><input type="text" name="rows"></td> 
    <td><input class="update" type="button" name="update" value="Update"></td> 
    <td class="inputCol"></td> 
</tr> 
</table> 

<table class="bottomTable"> 
    <tr> 
     <th class="textCol">Product</th> 
     <th class="textCol">Quantity</th> 
     <th class="textCol">Unit Price</th> 
     <th class="textCol">Total Price</th> 
    </tr> 
<? 
    if (isset($_POST['update'])) 
    { 
     //Execute this code if the update button is clicked. 
     $num = $_POST['rows']; 

     for ($i=0; $i<$num; $i++) { ?> 
      echo "<tr> 
        <td class="inputCol2"><input type="text" name="product[$i]" ></td> 
        <td class="inputCol2"><input type="text" name="quantity[$i]" ></td> 
        <td class="inputCol2">$<input type="text" name="unit[$i]" ></td> 
        <td class="inputCol2">$<input type="text" name="total[$i]" ></td> 
       </tr>"; 
    <? } ?> 
<? } else {?> 

    <tr> 
     <td class="textCol"></td> 
     <td class="textCol"></td> 
     <td class="textCol">Total Order:</td> 
     <td class="inputCol2">$<input type="text" name="total6"></td> 
    </tr> 
<? } ?> 

</table> 
+0

哪裏是要更新的代碼? –

回答

2

輸入的形式是?如果它不在表單中,它默認提交爲$ _GET,而不是$ _POST,所以if()總是會發現它爲FALSE。

+0

謝謝,我忘記了它默認爲$ _GET方法。我在表格周圍添加了表單標籤,並且將更新按鈕的輸入更改爲「提交」,因爲它只是「按鈕」,它的工作原理! – Haley

0
<form method="post"> 
<table> 
    <tr> 
     <th class="textCol">Product Rows:</th> 
     <td class="inputCol"><input type="text" name="rows"></td> 
     <td><input class="update" type="submit" name="update" value="Update"></td> 
     <td class="inputCol"></td> 
    </tr> 
    <tr> 
     <th class="textCol">Product</th> 
     <th class="textCol">Quantity</th> 
     <th class="textCol">Unit Price</th> 
     <th class="textCol">Total Price</th> 
    </tr> 
<? 
    if (isset($_POST['update'])) 
    { 
     //Execute this code if the update button is clicked. 
     $num = $_POST['rows']; 

     for ($i=0; $i<$num; $i++) { ?> 
      <tr> 
       <td class="inputCol2"><input type="text" name="'product' . <?=[$i]?>" ></td> 
       <td class="inputCol2"><input type="text" name="'quantity' . <?=[$i]?>" ></td> 
       <td class="inputCol2">$<input type="text" name="'unit' . <?=[$i]?>" ></td> 
       <td class="inputCol2">$<input type="text" name="'total' . <?=[$i]?>" ></td> 
      </tr> 
    <? } ?> 
<? } else {?> 
    <tr> 
     <td class="textCol"></td> 
     <td class="textCol"></td> 
     <td class="textCol">Total Order:</td> 
     <td class="inputCol2">$<input type="text" name="total6"></td> 
    </tr> 
<? } ?> 
</table> 
</form> 

對於我的價值觀的PHP仍然不正確,但我目前的工作。但它現在正確更新了行。感謝您的幫助!

相關問題