将多个文本框值传递给控制器​​代码点火器 [英] pass multiple textbox values to controller Code igniter

查看:111
本文介绍了将多个文本框值传递给控制器​​代码点火器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Codeigniter 的新手,所以我不知道该怎么做.
实际上,我的视图页面有五行是我通过for循环生成的,其中每一行包含两个选择框和两个输入框.我不知道如何首先在 jQuery 中获取这些值,然后再传递给控制器​​.实际上,我知道如何从简单的文本框中获取值,但是由于我是在循环中生成它们并为它们提供一个单独的ID,所以我不知道如何在jQuery中获取值然后传递给控制器​​.

I am newbie in Codeigniter so I don't know how to do this.
Actually my view page has five rows which I generated through for loop in which each row contain two select boxes and two input boxes. I don't know how to get these values first in jQuery and then pass into controller. Actually I know how to grab values from a simple textboxes but because I generate them in a loop and give them a separate id, so I don't know how to get the values in jQuery and then pass to the controller.

这是我的观点

       <tr>
        <th>Category:</th>
    <th>Items:</th>
       <th>Selling Price:</th>
        <th>quantity:</th>

</tr>

    <?php for ($i = 0; $i < 5; $i++) {

    ?>
    <tr> //Categories
<td><?php echo form_dropdown('cat_id', $records2, '#', 
        "id='category_".$i."'  onchange='getItems(this.value,".$i.")' ");?>
                        </td>
          <!-- Items -->

  <td>form_dropdown('item_id', $records3, '#', "id='items_".$i."'"); ?> </td>
    <!-- end of Items -->



    <td> <?php echo form_input($price); ?></td>

<td><?php echo form_input($quantity); }?></td>

项目选项基于类别选择,这意味着如果我从类别中选择某个选项,则针对类别的项目将显示在第二个选择框中.

The item options is based on the categories selection, means if I select some option from categories, then the items against categories show in a second select box.

这是我的Javascript,用于显示前两个选择框的功能

Here is my Javascript for functionality of top two select boxes

    <script type="text/javascript">

function getItems(category_id,index) {

       $("#items_" + index + " > option").remove(); 

    $.ajax({
        type: "POST",
        url: "stockInController/get_Items/"+category_id, 
        success: function(items) 
        {
            $.each(items,function(item_id,item_name) 
            {
                var opt = $('<option />'); 
                opt.val(item_id);
                opt.text(item_name);
                $('#items_'+ index).append(opt); 
            });
        }

    });
}
 </script> 

现在问题出在这里;我想从所有行中获取值,然后传递给控制器​​.
这是用于捕获然后将值发送到控制器的javascript

Now the problem is here; I want to get the values from all the rows and then pass into controller.
This is the javascript for catching and then sending values to controller

  <script type="text/javascript">
 $('#btns').click(function() { //  $("#form").serialize()

在这里,我做错了,首先我不知道如何在此处获取项目和类别的值,然后另一件事是如何能够获取多个文本框值,因为有五行,并且文本值值将被填充

Here, I am not doing right, first I don't know how to get values of item and categories here and then the other thing is how to be able to get multiple textbox values because there are five rows, and the textvalues in which values are filled are to be sent

var item_id = $('#items').val();   
var item_id = $('#items').val();


var price = $('#price').val();
var quantity = $('#quantity').val();




var form_data = {
        cat_id: $('#cat_id').val(),

        quantity: $('#quantity').val(),
        item_id: $('#items').val(),




};


$.ajax({
    url: "<?php echo site_url('salesController/addSales'); ?>",
    type: 'POST',
    data: form_data,
    dataType: 'json',
    success: function(msg) {
        if(msg.res == 1)
        {

            alert("true");
        }
        else{
            alert("false");          
          }


    }
});

return false;
    });


</script>

这是我的控制器,我也不知道在这里也获得多个值.
这是我正在做的代码,我知道我错了,但是我不知道如何做到正确

Here is my controller and I don't know to get multiple values here also.
Here is the code of what I am doing, I know I am wrong, but I don't know how to make it right

       $data = array(
            'cat_id' => $this->input->post('cat_id'),

            'item_id' => $this->input->post('item_id'),
            'price' => $this->input->post('sales_date'),
            'quantity' => $this->input->post('sales_bill_no'),

            );

推荐答案

在您的情况下,请设置inputselect元素的name,例如cat_ids[]item_ids[]prices[]quantities[].注意这些名称末尾的[](我已经更改了名称,使其以复数形式出现,这只是一个名称约定:)).

In your case, set the name of your input and select elements like this cat_ids[], item_ids[], prices[] and quantities[]. Note the [] at the end of these name (I have changed the name to make it in plural-form, this is just a name convention :) ).

在控制器中,$this->input->post('cat_ids')将返回一个名为cat_id数组,其中包含您的所有ID,对于$this->input->post('item_ids')$this->input->post('prices'),...来说都是相同的...您可以使用for这样的循环遍历所有发布的值:

In the controllers $this->input->post('cat_ids') will return an array named cat_id and contains all your ids, this is the same for $this->input->post('item_ids'), $this->input->post('prices'), ... You can use a for loop like this to traverse through all posted values:

for($i = 0; $i < count($data['cat_id']); $i++)
{
    $yourItem = array (
        'cat_id' => $data['cat_id'][$i],
        'item_id' => $data['item_id'][$i],
        'price' => $data['price'][$i],
        'quantity' => $data['quantity'][$i],
    );
}

要使用jQuery在Ajax中发送表单数据,您可以设置您的form_data=$('your-form-id').serialize();而不是手动创建它的值:)

To send the form data in Ajax uses jQuery you can set your form_data=$('your-form-id').serialize(); instead of building its value by your hand :)

这篇关于将多个文本框值传递给控制器​​代码点火器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆