0

i want to make a cart and my product is pizza. so i'm listing all the menu to the table, and add a button to submit menu to cart.

here's my index.php

<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>


<form action="cart.php" method="get">
    <table  border="1" style="width:100%">
        <tr>
            <th>Pizza Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Buy</th>
        </tr>
        <?php while($product = mysqli_fetch_object($result)){ ?>
            <tr>
                <td><?php echo $product->nama_menu; ?></td>
                <td><?php echo $product->harga_menu; ?></td>
                <td><input type="number" name="quantity" min="1" max="20"></td>
                <td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
            </tr>
        <?php } ?>
    </table>
</form>     

But when i pressed the button "Add To Cart", it sends all the quantity from the menu listing and can't be read in my cart.php

can anyone help me how to get the right quantity value when i pressed the button add to cart.

3
  • can you please show your output and your desire output. Commented Mar 28, 2016 at 7:24
  • 1
    You are keeping the same name for all inputs in the loop. Make it as array name="quantity[]" Commented Mar 28, 2016 at 7:26
  • The easiest way might be to use a simple javascript function attached to each button ( change it's type to button rather than submit ) Commented Mar 28, 2016 at 7:29

2 Answers 2

1

Make separate form for each of the item. Try below code.

<?php
require("connect.php");
$result = mysqli_query($con,'select * from menu');
?>



    <table  border="1" style="width:100%">
        <tr>
            <th>Pizza Name</th>
            <th>Price</th>
            <th>Quantity</th>
            <th>Buy</th>
        </tr>
        <?php while($product = mysqli_fetch_object($result)){ ?>
            <form action="cart.php" method="get">
            <tr>
                <td><?php echo $product->nama_menu; ?></td>
                <td><?php echo $product->harga_menu; ?></td>
                <td><input type="number" name="quantity" min="1" max="20"></td>
                <td><button type="submit" name="id" value="<?php echo $product->id_menu; ?>">Add To Cart</button></td>
            </tr>
            </form>
        <?php } ?>
    </table>
Sign up to request clarification or add additional context in comments.

Comments

0

Try to use array in name then submit it will give you seperate quantity.

<td><input type="number" name="quantity[<?php echo $product->nama_menu; ?>]" min="1" max="20">

Output: quantity['pizza1'] = 10 quantity['pizza2'] = 20 ....

Another Option is use dynamic name for number.

<td><input type="number" name="quantity_<?php echo $product->nama_menu; ?>" min="1" max="20">

Output: quantity_pizza1 = 10 quantity_pizza2 = 20 ....

3 Comments

Is this useful to you or not. Please feedback us. @Shasapo
i try this too, it works, but i want to keep it simple and not using array. thankyou for your answer :D
Yes so I have added another option which is best for you. @Shasapo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.