2014-07-09 76 views
1

代碼order.html.php:在顯影工具呈現改變DOM當加入輸入字段形式的PHP

<body> 
<p> Place order</p> 

<table> 
    <thead> 
     <tr> 
      <th>Item No.</th> 
      <th>Name</th> 
      <!--<th>Mrp</th>--> 
      <th>Price</th> 
      <th>Quantity</th> 
     </tr> 
    </thead> 
    <form id="form1" action="" method="post" > 
     <tbody> 
      <?php $id = 0 ;foreach ($dataProduct as $productData): ?> 
      <tr> 
       <td><?php echo ++$id ; ?></td> 
       <td><?php echo $productData['productName']; ?></td> 
       <td><?php echo $productData['productPrice'];?></td> 
       <td><input type="number" name="<?php echo $productData['productId']; ?>"/></td> 
      </tr> 
     <?php endforeach; ?> 
     <div><input type="hidden" name="add" value="placeorder"/> 
      <input type="submit" value="Place Order"/> 
      <div> 
      </tbody> 
     </form> 
    </table> 
</body> 

和DOM是:

<body> 
<p> Place order</p> 

<div><input type="hidden" name="add" value="placeorder"> 
      <input type="submit" value="Place Order"> 
      <div> 
      </div></div> 
<table> 
    <thead> 
     <tr> 
      <th>Item No.</th> 
      <th>Name</th> 
      <!--<th>Mrp</th>--> 
      <th>Price</th> 
      <th>Quantity</th> 
     </tr> 
    </thead> 
    <form id="form1" action="" method="post" ></form> 
     <tbody> 
          <tr> 
       <td>1</td> 
       <td>hp keyboard old</td> 
       <td>400</td> 
       <td><input type="number" name="1"></td> 
      </tr> 
         <tr> 
       <td>2</td> 
       <td>lenovo keyboard old</td> 
       <td>450</td> 
       <td><input type="number" name="2"></td> 
      </tr> 
         <tr> 
       <td>3</td> 
       <td>kaspersky antivirus</td> 
       <td>430</td> 
       <td><input type="number" name="9"></td> 
      </tr> 
        </tbody> 

    </table> 

    </body>  

看到的位置提交併爲了。 html.php和導致的dom。 由於這種dom結構,order.html.php無法提交駐留在輸入字段中的值。

這裏是後與相關的問題,但沒有什麼是代表在這裏, Retrieve data from HTML table to PHP

+1

您的HTML無效,這就是原因。 –

+0

html有效。現在看@think – devprashant

+0

不,它不是@devprashant。看看你的HTML *** >>> ***你不能把表格標籤放在thead後面,你不能在關閉tr –

回答

0

嘗試正確的HTML結構:

 <body> 
     <p> Place order</p> 
     <form id="form1" action="" method="post" > 
     <table> 
      <thead> 
       <tr> 
        <th>Item No.</th> 
        <th>Name</th> 
        <!--<th>Mrp</th>--> 
        <th>Price</th> 
        <th>Quantity</th> 
       </tr> 
      </thead> 

       <tbody> 
        <?php $id = 0 ;foreach ($dataProduct as $productData): ?> 
        <tr> 
         <td><?php echo ++$id ; ?></td> 
         <td><?php echo $productData['productName']; ?></td> 
         <!--<td><?php echo $productData['mrp']; ?></td>--> 
         <td><?php echo $productData['productPrice'];?></td> 
         <td><input type="number" name="<?php echo $productData['productId']; ?>"/></td> 
        </tr> 
       <?php endforeach; ?> 
       <tr> 
       <td colspan="4"> 
        <input type="hidden" name="add" value="placeorder"/> 
        <input type="submit" value="Place Order"/> 
       </td></tr>     
      </tbody> 

      </table> 
     </form> 
</body> 
+0

它不起作用 – devprashant

+0

現在生成的代碼是什麼? –

+0

與之前相同。看到上面有人給出了它背後的原因。 – devprashant

0
  • 首先,只是包裝表與<form>標籤。其次,使用productId作爲name="$productId"並不是很好,而是使用類似name="products[$productId]"的東西,這樣在表單提交後,它們將與數組分組在同一變量上。

示例代碼:Sample Demo

$dataProduct = array(
    array('productId' => 1, 'productName' =>'hp keyboard old', 'productPrice' => 400), 
    array('productId' => 2, 'productName' =>'lenovo keyboard old', 'productPrice' => 430), 
    array('productId' => 9, 'productName' => 'kaspersky antivirus', 'productPrice' => 430), 
); 

if(isset($_POST['submit'])) { 
    $values = $_POST['products']; // get all the grouped values 
    $total = array(); 
    foreach($values as $productId => $quantity) { // loop values 
     foreach($dataProduct as $productData) { 
      if($productData['productId'] == $productId) { 
       // simple multiplication, quantitiy times price for each item 
       $total[$productId] = $productData['productPrice'] * $quantity; 
      } 
     } 
    } 

    echo '<pre>'; 
    print_r($total); 
    echo '</pre>'; 
} 

?> 

<form method="POST"> 
<table cellpadding="10"> 
    <thead> 
     <tr> 
      <th>Item No.</th> 
      <th>Name</th> 
      <!--<th>Mrp</th>--> 
      <th>Price</th> 
      <th>Quantity</th> 
     </tr> 
    </thead> 
    <tbody> 
     <?php $id = 0 ; foreach($dataProduct as $productData): ?> 
     <tr> 
      <td><?php echo ++$id ; ?></td> 
      <td><?php echo $productData['productName']; ?></td> 
      <td><?php echo $productData['productPrice'];?></td> 
      <td><input type="number" name="products[<?php echo $productData['productId']; ?>]"/></td> 
     </tr> 
     <?php endforeach; ?> 
     <tr> 
      <td><input type="submit" name="submit" /></td> 
     </tr> 
    </tbody> 
</table> 
</form> 

因此,例如,在形式上,我在所有的文本框輸入的2. print_r()量應該產生這樣的:

Array 
(
    [1] => 800 
    [2] => 860 
    [9] => 860 
) 

指數作爲鑰匙,數值是每個數量乘以價格的總和