1

code for order.html.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>

and dom presented in developer tool is:

<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>    

see the position of submit and in order.html.php and the resulted dom. because of this dom structure order.html.php is not able to submit value that resides in input filed.

here is the post with related issue but not exactly what is represent here, Retrieve data from HTML table to PHP

Community
  • 1
  • 1
devprashant
  • 1,285
  • 1
  • 13
  • 23

2 Answers2

0

Try proper html structure :

     <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>
Himanshu Tyagi
  • 5,201
  • 1
  • 23
  • 43
0
  • First off, just wrap the table with the <form> tag.
  • Second, it's not really good to use productId as your name="$productId", instead, use something like name="products[$productId]" so that after form submission, they will be grouped on the same variable as an array.

Sample Code: 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>

So for example, in the form, I inputted in all of textboxes a quantity of 2. print_r() should yield like this:

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

Indices as the keys, the values are the totals for each quantity times price

user1978142
  • 7,946
  • 3
  • 17
  • 20