PHP处理组合复选框和选择框

时间:2016-03-18 08:45:39

标签: php checkbox

我正在处理将图像上传到服务器的表单。表单包含复选框和格式为<table>的选择框。每个表行包含两列。 First列遍历一个mysql查询,该查询显示与复选框配对的项目列表(确切地说是24项)。第二列迭代显示客户端列表的mysql查询。

screenshot here

需要做的事情如下:

  1. 当表单上传(通过POSTGET)时,只会检查通过的复选框。
  2. subs_id(选择框的名称参数)转换为数组,以便于mysql insert处理。
  3. 以下代码。先谢谢!

    <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>
    

3 个答案:

答案 0 :(得分:0)

<input type="checkbox" id="checkAll" name='client[]' value ="1" />&nbsp;&nbsp;<?php echo $item['name']; ?>

缺少名称和值

答案 1 :(得分:0)

1.将您的复选框代码更改为

<input type="checkbox" id="checkAll" name="checkId[]" value="<?php echo $item['name']; ?>"/>&nbsp;&nbsp;<?php echo $item['name']; ?>

在这里,我将名称作为数组checkId[]和复选框value="<?php echo $item['name']; ?>"

的值

这应该适合你。

  1. 您的选择框很好。没有必要改变它。你可以通过检查索引找到选择框的值。

答案 2 :(得分:0)

我明白了。我所做的是检测选择框何时发生更改并将选择框的值分配给复选框的值。

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

然后,在upload2.php上,我使用下面的代码来获取仅带有支票的方框的值

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']))
瞧,瞧!为我工作的代码。

相关问题