phpcheckbox

PHP handling combined checkboxes and select boxes


I'm working on a form that uploads an image to a server. The form contains checkboxes and select boxes formatted to a <table>. Each table row contains two columns. The First column iterates through a mysql query that displays a list of items (24 items to be exact) that is paired to a checkbox. The 2nd column iterates through a mysql query that displays list of clients.

screenshot here

What needs to be done are as follows:

  1. When the form gets uploaded (via POST or GET) the checkboxes which only have check gets passed.
  2. convert the subs_id (name parameter for the select box) to an array for easy processing for mysql insert.

Code Below. Thanks in advance!

<form method="post" enctype="multipart/form-data" action="upload2.php">
        Name: <input type="text" name="item_desc" value=""> 
        Show Date: <input type="text" name="upload_date" class="date" size="10"> to <input type="text" name="expiry_date" class="date" size="10">
        Item: <input type="file" name="item"> Display Length: <input type="number" value="8" name="show_time" size="3" style="width:50px"> secs 
        
        
        <table class="data-list" width="100%">
            <tr>
                <td>Widget</td>
                <td>Client</td>
            </tr>   
            
            <?php foreach($instance as $item):?>
            <tr>
                <td><input type="checkbox" id="checkAll"/>&nbsp;&nbsp;<?php echo $item['name']; ?></td>
                <td><select id="selectBox" name="subs_id[]">
                    <option selected="selected" value="none">Select Client</option>
                    <?php foreach($subs as $client): ?>         
                        <option value="<?php echo $item['id'] . '-' . $client['id']; ?>" client="<?php echo $client['client_name']; ?>" ><?php echo $client['client_name']; ?></option>
                    <?php endforeach ?>
                    </select>
                </td>
            </tr>   
            <?php endforeach; ?>
        </table>        
        <br>
        <input type="submit" value="upload">
    </form>

Solution

  • I figured it out. What I did was detect when there was a change on the select box and assign the value of the select box to the check box's value.

    $(".selectBox").change(function() {
        $(this).parent().siblings().children('input').attr('value',$(this).val());
    });
    

    then, on the upload2.php, I used the code below to get the value of only the boxes with check

    if(!empty($_POST['checkId'])) {
                $filename = $this->unique_filename($this->extract_ext( $_FILES["item"]["name"]));
                $folder = dirname(__FILE__) . '/items';
                move_uploaded_file($_FILES['item']['tmp_name'],"{$folder}/{$filename}");
    
    
                foreach($_POST['checkId'] as $check) {
                    //save to db
                    $split = explode("-",$check);
                    $subs_id = $this->use_table('clients')->where('client_name like "' . $split[2] . '%"')->where('site_id=' . $split[1])->fetch();
                    //$subs_id = $this->query('select * from clients where client_name like "Trinoma%" and site_id=1');
                    //print_r($subs_id[0][id]);
                    $data = array(
                    'item_name'=> 'widgets/' . strtolower(get_class($this)) . '/items/' . $filename,
                    'show_time'=> $_POST['show_time'],
                    'instance_id'=> $split[0],
                    'item_desc'=>$_POST['item_desc'],
                    'subs_id'=> $subs_id[0][id]
                    );
    
                    $this->use_table(TBL_NAME)->insert($data)->execute();
                    $mid = $this->last_insert_id();
                    $this->use_table(TBL_SCHED)->insert(array(
                                                    'multirotator_id'=>$mid,
                                                    'show_from'=>'00:00',
                                                    'show_to'=>'23:59',
                                                    'upload_date'=>$_POST['upload_date'],
                                                    'expiry_date'=>$_POST['expiry_date']                                                
                                            ))->execute();
                } //end of foreach
            } //end of if(!empty($_POST['checkId']))
    

    and voila! working code for me.