将选定的复选框下拉值发送到Codeigniter

时间:2018-03-01 15:06:02

标签: php mysql database codeigniter

我正在使用Codeigniter和Mysql。我试图向mysql表发送多个复选框下拉值。但只有一个价值有价值。它是去数据库的最后一个值。我也尝试过其他一些方法。但最后我在这里。提前谢谢。

以下是视图。



<div class="form-group">
                    <select class="form-control"  data-toggle="dropdown"></select>
                    <ul class="dropdown-menu">
                        <li>
                            <a href="#" name="sub_cat_id" id="sub_cat_id" class="small" data-value="option1" tabIndex="-1">
                                <input name="sub_cat_id" id="sub_cat_id"  type="checkbox"/ ><?php echo $this->lang->line('select_sub_cat_message')?>
                            </a>
                        </li>
                </div>
                
                <script>
    $(document).ready(function(){
        $('#item-form').validate({
            rules:{
                name:{
                    required: true,
                    minlength: 4,
                    remote: {
                        url: '<?php echo site_url("items/exists");?>',
                        type: "GET",
                        data: {
                            name: function () {
                                return $('#name').val();
                            },
                            sub_cat_id: function() {
                                return $('#sub_cat_id').val();
                            }
                        }
                    }
                },
                unit_price: {
                    number: true
                }
            },
            messages:{
                name:{
                    required: "Please fill item Name.",
                    minlength: "The length of item Name must be greater than 4",
                    remote: "Item Name is already existed in the system"
                },
                unit_price: {
                    number: "Only number is allowed."
                }
            }
        });

        $('#cat_id').change(function(){
            var catId = $(this).val();
            $.ajax({
                url: '<?php echo site_url('items/get_sub_cats');?>/'+catId,
                method: 'GET',
                dataType: 'JSON',
                success:function(data){
                    $('#sub_cat_id').html("");
                    $.each(data, function(i, obj){
                     //   $('#sub_cat_id').append('<option value="'+ obj.id +'">' + obj.name + '</option>');
                        $('#sub_cat_id').append('<input name="sub_cat_id" type="checkbox" value="'+ obj.id +'"/>&nbsp;' + obj.name + '');
                    });
                    $('#name').val($('#name').val() + " ").blur();
                }
            });
        });

        $('#sub_cat_id').on('change', function(){
            $('#name').val($('#name').val() + " ").blur();
        });

        $(function () { $("[data-toggle='tooltip']").tooltip(); });
    });
</script>
                
                
&#13;
&#13;
&#13;

这是控制器功能

&#13;
&#13;
function add()
	{
		if(!$this->session->userdata('is_shop_admin')) {
		      $this->check_access('add');
		}
		
		$action = "save";
		unset($_POST['save']);
		if (htmlentities($this->input->post('gallery'))) {
			$action = "gallery";
			unset($_POST['gallery']);
		}
		
		if ($this->input->server('REQUEST_METHOD')=='POST') {

			$item_data = array();
			foreach ( $this->input->post() as $key=>$value) {
				$item_data[$key] = htmlentities($value);
			}

			$item_data['shop_id'] = $this->get_current_shop()->id;
			$item_data['is_published'] = 1;
			
			//unset($item_data['cat_id']);
			
			if ($this->item->save($item_data)) {			
				$this->session->set_flashdata('success','Item is successfully added.');
			} else {
				$this->session->set_flashdata('error','Database error occured.Please contact your system administrator.');
			}
			
			if ($action == "gallery") {
				redirect(site_url('items/gallery/'.$item_data['id']));
			} else {
				redirect(site_url('items'));
			}
		}
		
		$cat_count = $this->category->count_all($this->get_current_shop()->id);
		$sub_cat_count = $this->sub_category->count_all($this->get_current_shop()->id);
		
		if($cat_count <= 0 && $sub_cat_count <= 0) {
			$this->session->set_flashdata('error','Oops! Please create the category and sub category first before you create items.');
			redirect(site_url('items'));	
		} else {
			if($cat_count <= 0) {
				$this->session->set_flashdata('error','Oops! Please create the category first before you create items.');
				redirect(site_url('items'));
			} else if ($sub_cat_count <= 0) {
				$this->session->set_flashdata('error','Oops! Please create the sub category first before you create items.');
				redirect(site_url('items'));
			}
		}
		
		$content['content'] = $this->load->view('items/add',array(),true);
		$this->load_template($content);
	}
&#13;
&#13;
&#13;

这是模型函数

&#13;
&#13;
	function save(&$data, $id = false)
	{
		if (!$id && !$this->exists(array('id' => $id, 'shop_id' => $data['shop_id']))) {
			if ($this->db->insert($this->table_name, $data)) {
				$data['id'] = $this->db->insert_id();
				return true;
			}
		} else {
			$this->db->where('id', $id);
			return $this->db->update($this->table_name, $data);
		}	
		return false;
	}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

修改1 忘了查看显而易见的,在视图中查看

 <a href="#" name="sub_cat_id" 
  id="sub_cat_id" 
  class="small" data-value="option1" 
  tabIndex="-1">
  <input name="sub_cat_id" id="sub_cat_id"  type="checkbox"/ >
    <?php echo $this->lang->line('select_sub_cat_message')?>
  </a>

U具有<a><input>标记的相同名称。

<强> ____________________________________________________________________

您要将此信息发送到服务器

<input name="sub_cat_id" id="sub_cat_id" type="checkbox"/ > <?php echo $this->lang->line('select_sub_cat_message')?> </a>

问题在于name="sub_cat_id"它是一个字符串,你需要一个数组,解决此问题的一种方法是将名称重命名为name="sub_cat_id[]",并且要在控制器功能中检索它,你可以使用

function functionThatProcessesTheForm(){
     $subCatIds = $this->input->post('sub_cat_id');
     /** 
       subCatIds its an array, the value of each element will be
       the "value" the input had, if it wasn't selected, it will not be
       contained in the array 
     **/
}

重要

如果输入value ="theidvalue",请设置值,如果您的ID不会到达控制器,如果设置了值,您将找到[1,1,1,1,1,1]之类的数组,你的数组将是[id1,id2,id3,...,idn]

希望我的回答可以帮到你。