0
<?php
    $sql = "SELECT * FROM product ORDER BY product_id DESC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

            echo "" . $row["product"] . "\n\n<input type='checkbox' name='product[]' value=" .$row["product"] . ">\n         
     Quantity\n\n<select name='quantity[]'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
    </select><br />";
        }

    ?>
    <button name="submit" type="submit">submit</button><br />

    <?php

        if (isset($_POST['submit'])) {
            if (is_array($_POST['product'])) {
                foreach ($_POST['product'] as $key => $product_name) {
                    $quantity = $_POST['quantity'][$key];
                    echo $product_name . "\n";
                    echo $quantity . "<br>";

                }
            }
        }
    }

    ?>

i would want to select multiple product and quantity but it doesn't giving me quantity value what i select.

example picture1
on example picture1 you could see i selected 2 products

  • Asus 2
  • Acer 3

But if click on submit i get result like
example picture2

  • Asus 1
  • Acer 1
7
  • The first and second dropdown have value 1 so the results are correct. All quantity values are always submitted. Commented Nov 25, 2016 at 22:55
  • 1
    Your issue is that only checked checkboxes are posted, but all selects are posted. So you need to need to link the 2 together so that they keys match. You can add a simple counter var, ie. $i=0, name='product[$i]'/name='quantity[$i]' Commented Nov 25, 2016 at 22:56
  • did you see what product i selected? @Maarten van Middelaar Commented Nov 25, 2016 at 22:58
  • i appreciate your help but im just a learner, not expert on php. Would you please correct my script and post it here? @Sean Commented Nov 25, 2016 at 23:06
  • 1
    @MaartenvanMiddelaar posted an answer that is equivalent to what I mentioned. My example was to use a simple counter var, where he used $row['product_id'] (or whatever your row id column name is). Commented Nov 25, 2016 at 23:09

1 Answer 1

1

Try using the product ID to link the checkboxed to the select boxes.:

$sql = "SELECT * FROM product ORDER BY product_id DESC";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while ($row = $result->fetch_assoc()) {

            echo "" . $row["product"] . "\n\n<input type='checkbox' name='product[".$row['product_id']."]' value=" .$row["product"] . ">\n         
     Quantity\n\n<select name='quantity[".$row['product_id']."]'>
        <option value='1'>1</option>
        <option value='2'>2</option>
        <option value='3'>3</option>
    </select><br />";
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect... You are amazing. Thank you.

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.