Search code examples
phphtmlformsloopsdata-processing

Form send data to php with multiple inputs with the same name


I'm trying make a form where me or user can insert data. Some parts are coming form data base like: Work name and work price. In input fields can insert work amount. In each input row have checkbox, if checkbox ar checked, that row will be seen in php.

<table class="table">

        <thead>
            <tr>
                <th scope="col">*</th>
                <th scope="col">Work name</th>
                <th scope="col">Quantity</th>
                <th scope="col">Price for unit</th>
                <th scope="col">Total price</th>
            </tr>
        </thead>

        <form method="POST" action="process/pdf.process.php">
        <tbody>
            <?php
            $works = ORM::for_table('work')->find_many();
            foreach($works as $work){
            ?>
                <tr id="<?php echo $work->id; ?>">
                    <td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
                    <td><?php echo $work->name; ?></td>
                    <td><input type="text" placeholder="" class="amount"/> <?php echo $work->unit; ?></td>
                    <td class="work_price" data="<?php echo $work->price; ?>"><?php echo $work->price.'€/'.$work->unit; ?></td>
                    <td class="total_price">0€</td>

                    <input type="" name="work_id[]" value="<?php echo $work->id; ?>" />
                    <input type="hidden" name="work_name[]" value="<?php echo $work->name; ?>" />
                    <input type="hidden" name="amount[]" class="<?php echo $work->id; ?>_copy_amount" value="" />
                    <input type="hidden" name="unit[]" value="<?php echo $work->unit; ?>" />
                    <input type="hidden" name="unit_price[]" value="<?php echo $work->price; ?>€" />
                    <input type="hidden" name="unit_total[]" class="<?php echo $work->id; ?>_copy_total" value="" />

                </tr>

            <?php
            }
            ?>
        </tbody>
            <input type="submit" name="do_pdf" value="Pga jkāuztaisa ar jquery" />
        </form>

    </table>

Now, there is php, but how can i show only checked rows in while loop?

<?php

 $data = array();
 $work_id = array();
 $work_names = $_POST['work_name'];
 $amounts = $_POST['amount'];
 $units = $_POST['unit'];
 $units_prices = $_POST['unit_price'];
 $units_total = $_POST['unit_total'];


 if(isset($_POST['useit'])){
  $work_id = $_POST['useit'];


 }

 $data = array($work_id, $work_names, $amounts, $units, $units_prices, $units_total);

 echo '<pre>';
 echo htmlspecialchars(print_r($data, true));
 echo '</pre>';

 ?>

Solution

  • There are different possibilities how to do that. One Suggestion (i limit it to the parts which are relevant)

    form (note that i gave work_name the id as an index, use_it not (but it could have)

    <td><input type="checkbox" name="useit[]" value="<?php echo $work->id; ?>"/></td>
    <input type="hidden" name="work_name[<?php echo $work->id?>]" value="<?php echo $work->name; ?>" />
    

    The form only submits the checked checkboxes values in an array, all other are omited. Therefore we could loop over the checked checkbox values like this

    foreach($_POST['useit'] as $work_id){
        $work_name = $work_names[$work_id]; 
        //do something with the checked rows only (all others are not looped over)
    }
    

    This is only possible, due to the given id as an array key in the form work_name[<?php echo $work->id?>].

    A general sidenote for better (and more secure) code: please note that your data could be modified by the user, and send back with wrong data (or worse). So please make sure to sanitize your input, or probably better in this case only submit the id in question and the new data and pickup the rest directly from your database. So you can make sure the hidden data has not been modified on the client side and send back wrong.