这是我的观点我使用jquery提交表单,然后我想检查点击了哪个按钮'添加产品'或控制器中的SUBMIT按钮
<?php echo form_open_multipart('itemcontroller/submit'); ?>
<div class="col-md-6">
<div class="add-name">
<label>Product Name</label>
<input type="text" id="product_name" name="product_name" placeholder="Product name">
<span id="pnerror" style="display: none;">Please enter Product Name</span>
</div>
<div class="add-name">
<label>Product Image</label>
<input type="file" id="user_file" name="user_file">
<span id="imgerror" style="display: none;">Please select an Image</span>
</div>
<div class="add-name">
<label>Product Category</label>
<?php $attributes = 'id="cat"';
echo form_dropdown('cat', $cat, set_value('cat'), $attributes); ?>
<span id="caterror" style="display: none;">Please select Category</span>
</div>
</div>
<div class="col-md-6">
<div class="add-name">
</div>
<div class="add-name">
<label>Product Description</label>
<textarea class="form-control" rows="8" id="product_description" name="product_description" placeholder="Product Description"></textarea>
<span id="pderror" style="display: none;">Please enter Product Description</span>
</div>
</div>
<div class="sub-add">
<button type="submit" form="form1" id="fill" name="fill" value="add">ADD PRODUCT</button>
</div>
<div class="sub-add">
<button type="submit" id="go" name="go" form="form1" value="go">SUBMIT</button>
</div>
<?php echo form_close(); ?>
这是我的ADD PRODUCT按钮的jquery代码。
<script type="text/javascript">
$('#fill').click(function(event){
var product_name = $('#product_name').val();
var image = $('#user_file').val();
var cat = $('#cat').val();
var product_description = $('#product_description').val();
if(product_name.length == 0)
{
$('#pnerror').show();
}
if(image.length == 0)
{
$('#imgerror').show();
}
if(cat == 0)
{
$('#caterror').show();
}
if(product_description.length == 0)
{
$('#pderror').show();
}
else if(product_name.length != 0 && image.length != 0 && cat != 0 && product_description.length != 0)
{
var $target = $( event.target );
$target.closest("form").submit();
}
});
</script>
这是我的提交按钮
<script type="text/javascript">
$('#go').click(function(event){
var product_name = $('#product_name').val();
var user_file = $('#user_file').val();
var cat = $('#cat').val();
var product_description = $('#product_description').val();
if(product_name.length == 0)
{
$('#pnerror').show();
}
if(user_file.length == 0)
{
$('#imgerror').show();
}
if(cat == 0)
{
$('#caterror').show();
}
if(product_description.length == 0)
{
$('#pderror').show();
}
else if(product_name.length != 0 && user_file.length != 0 && cat != 0 && product_description.length != 0)
{
var $target = $( event.target );
$target.closest("form").submit();
}
});
</script>
这是我的控制器代码:
public function submit()
{
print_r($_POST);
if($this->input->post('go'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 2048;
$config['max_height'] = 1024;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('user_file'))
{
$data = array('prod_data' => $this->upload->data());
$user_file = $data['prod_data']['raw_name']."".$data['prod_data']['file_ext'];
$id = $this->session->userdata['record']['id'];
$com_id = $this->session->userdata['record']['com_id'];
$sam = array(
'title' => $this->input->post('product_name'),
'type' => 'product'
);
$this->load->model('registermodel');
$chk = $this->registermodel->insertsam($sam);
if ($chk == TRUE)
{
$id = $this->registermodel->getsampleid();
$data = array(
'product_name' => $this->input->post('product_name'),
'image' => 'images/'.$user_file,
'product_description' => $this->input->post('product_description'),
'cat_id' => $this->input->post('cat'),
'com_id' => $com_id,
'id' => $id
);
$check = $this->registermodel->insertproduct($data);
if($check == TRUE)
{
echo "<script>
alert('Data Submitted Succesfully');
</script>";
redirect('/homecontroller');
}
else
{
echo "Value insertion Failed";
}
}
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
else if($this->input->post('fill'))
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 2048;
$config['max_height'] = 1024;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('user_file'))
{
$data = array('prod_data' => $this->upload->data());
$user_file = $data['prod_data']['raw_name']."".$data['prod_data']['file_ext'];
$id = $this->session->userdata['record']['id'];
$com_id = $this->session->userdata['record']['com_id'];
$sam = array(
'title' => $this->input->post('product_name'),
'type' => 'product'
);
$this->load->model('registermodel');
$chk = $this->registermodel->insertsam($sam);
if ($chk == TRUE)
{
$id = $this->registermodel->getsampleid();
$data = array(
'product_name' => $this->input->post('product_name'),
'image' => 'images/'.$user_file,
'product_description' => $this->input->post('product_description'),
'cat_id' => $this->input->post('cat'),
'com_id' => $com_id,
'id' => $id
);
$check = $this->registermodel->insertproduct($data);
if($check == TRUE)
{
echo "<script>
alert('Product added Succesfully');
</script>";
$data['category'] = $this->linkmodel->get_category();
$data['cat'] = $this->registermodel->get_category();
$this->load->view('itemview',$data);
}
else
{
echo "Value insertion Failed";
}
}
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
}
}
}
的print_r($ _ POST); 这只返回: 数组([product_name] =&gt;多功能打印机[cat] =&gt; 82 [product_description] =&gt;大约价格:80,000卢比/件)
不返回ADD PRODUCT或SUBMIT按钮名称,请有人告诉我我做错了什么?
答案 0 :(得分:0)
您可能必须使用<input type="submit" ...>
代替<button ...>
。它应该将按钮的名称和值附加到$_POST
。
答案 1 :(得分:0)
在表单中添加input
hidden
字段,并在每个JavaScript代码中设置不同的值。
<?php echo form_open_multipart('itemcontroller/submit'); ?>
<!-- your existing code --->
<input type="hidden" name="submit_type" id="submit_type" value="" />
<?php echo form_close(); ?>
JavaScript代码修改
<script type="text/javascript">
$('#go').click(function(event){
<!-- your existing code --->
$('#submit_type').val('SUBMIT');
var $target = $( event.target );
$target.closest("form").submit();
}
});
</script>
<script type="text/javascript">
$('#fill').click(function(event){
<!-- your existing code --->
$('#submit_type').val('ADD PRODUCT');
var $target = $( event.target );
$target.closest("form").submit();
}
});
</script>