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

问题描述 投票:0回答:1

我正在使用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>
                
                

这是控制器功能

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);
	}

这是模型功能

	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;
	}
php mysql database codeigniter
1个回答
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]

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

© www.soinside.com 2019 - 2024. All rights reserved.