Skip to content
Advertisement

How to associate checbox array to the quantity array in PHP form

I have a box[] checkbox form with associated selected quantity[] after submitting, if I did not check the first checkbox, I can not get the quantity corresponding to the checked box.

here is my codes

<input class="form-check-input" name="box[0]" type="checkbox" value="DVD Shipper" id="flexCheckDefault">
                                    <label class="form-check-label" for="flexCheckDefault">DVD Shipper</label>
                                </div>
                                <div class="invalid-feedback">Choose at least one box</div>
                            </div>
                            <div class="divTableCell">
                                <select class="form-select" name="quantity[]">
                                    <option value="">Quantity</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                </select>
                                <div class="invalid-feedback">The quantity is required!</div>
                            </div>
                        </div>
                        <div class="divTableRow">
                            <div class="divTableCell">
                                <div class="form-check">
                                   
                                    <input class="form-check-input" name="box[1]" type="checkbox" value="Small Security" id="flexCheckDefault">
                                    <label class="form-check-label" for="flexCheckDefault">Small Security</label>
                                </div>
                                <div class="invalid-feedback">Choose at least one box</div>
                            </div>
                            <div class="divTableCell">
                                <select class="form-select" name="quantity[]">
                                    <option value="">Quantity</option>
                                    <option value="1">1</option>
                                    <option value="2">2</option>
                                    <option value="3">3</option>
                                </select>
                                <div class="invalid-feedback">The quantity is required!</div>
                            </div>
                        </div>

PHP Codes:

if(isset($_POST['submit'])){
    $ct=0;
    $stationName=$_POST['stationname'];
    foreach( $_POST['box'] as $k=> $value ){ // loop through array
        $station = $stationName;
        $box_name = addslashes( $value );  // set name based on value
        $quantity     = addslashes($_POST['quantity'][$ct] ); // set qty using $ct to identify # out of total submitted
        $db->query("INSERT INTO products (station, box, quantity) VALUES('".$station."', '".$box."', '".$quantity."')");
        $ct++; // increment +1
    }
}

I excuse my self if did not explain well my problem, just to let you know English is not my first language.

Advertisement

Answer

I think I figured out how to handle this issue and here are the codes

if (isset($_POST['add'])) {
    if(!empty($_POST['box'])){
    $quantityArray = array_filter($_POST['quantity'],"quantityFilter");
    $stationName = $_POST['stationname'];
    $inserted= false;
    foreach($_POST['box'] as $boxes => $values ){ // loop through array
        $k = (int)$boxes;
        $station = $stationName;
        $box  = addslashes($values);  // set name based on value
        $quantity = addslashes($quantityArray[$k] ); // set qty using $ct to identify # out of total submitted
         // same as set qty
         
        $db->insert($station, $box, $quantity);
   
        $k++; // increment +1
        $inserted = true;
    }
    ```
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement