接收和处理多维数组 [英] Receiving and Processing a multi dimensional array

查看:124
本文介绍了接收和处理多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我也问过这个问题两次,没有令人满意的答案,那就是,这个问题我面临两个问题前仍,没有因为我怎么回事这个进行更明智的。

I have asked this question twice, with no satisfactory answer, that is, the problem I faced two questions ago still remains and none the wiser as how I am going to proceed with this.

这一次,我将提供一切code和我收到,加上那里的问题发生,我知道问题是什么,但因为我是比较新的使用AJAX它有我疑惑的是什么。

This time I will provide all the code and what I receive, plus where the problem is occurring, I know what the issue is but since I'm relatively new to using AJAX it has me puzzled.

我有一个表格,当你点击其中倒数第二场,重复本身,与其他用户所有的ID得到改变是唯一的帮助。

I have a form, which when you click the second to last field, duplicates itself, with the help of another user all the id's get changed to be unique.

<form role="form" class="batchinvoice form-horizontal" id="batchinvoice">
                            <div class="row">
                                <div class="form-group col-md-2">
                                    <select class="form-control" name="sl_propid" >
                                        <?php foreach($property as $row) :?>
                                        <option value="<?php echo $row['id']; ?>">
                                            <?php echo $row[ 'property_name'] . " " . $row[ 'property_address1']; ?>
                                        </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="form-group col-md-1">
                                    <input type="text" name="sl_date" placeholder="Date" class="sl_date form-control" id="datepicker">
                                </div>
                                <div class="form-group col-md-2">
                                    <input type="text" name="sl_ref" placeholder="Invoice Reference" class="form-control" id="sl_ref">
                                </div>
                                <div class="form-group col-md-2">
                                    <select class="form-control" name="sl_nom_id" id="sl_nom_id">
                                        <?php foreach($subitem_nominals as $row) :?>
                                        <option value="<?php echo $row['subnom_id']; ?>">
                                            <?php echo $row[ 'nom_subitem_name']; ?>
                                        </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="form-group col-md-2">
                                    <input type="text" name="sl_desc" id="sl_desc" placeholder="Invoice Description" class="form-control">
                                </div>
                                <div class="form-group col-md-2">
                                    <input type="text" name="sl_vat" placeholder="VAT Amount" class="vat form-control" id="vat">
                                </div>
                                <div class="form-group col-md-1">
                                    <input type="text" name="sl_net" placeholder="Net" class="form-control" id="sl_net">
                                </div>
                        </form>

JS的为形式的克隆

The JS for the form clone

<script>
var clone_iterator = 0;
$(function () {
    var i = 0;
    $.datepicker.setDefaults($.datepicker.regional['']);
    $.datepicker.formatDate("yy-mm-dd");
    $('#datepicker').datepicker();



    $('#batchinvoice .vat').click(function () {
       // alert("ok");
        var id = "batchinvoice" + clone_iterator;
        $('#batchinvoice').clone(true).attr("name", id).attr('id', id).appendTo(".panel-body"); // here I added this .attr('id',id)
        // here we'll change the id's of all the elements who actualy have an attribute

        $('#' + id + ' *[id]').each(function () {

            $(this).attr('id', $(this).attr('id') + clone_iterator); //we set the attribute id
        });
        clone_iterator++;
    });



    $('#submit').click(function () {
        var res = {};
        console.log(res);
        $('.batchinvoice').each(function (i) { //the class is a good use to do things like that you can repeat it more than once !
            res[i] = new Array();
            console.log(res);
                     res[i].propid = $(this).find('*[name=sl_propid]').val();
            res[i].date = $(this).find('.sl_date formcontrol').val();
            res[i].ref = $(this).find('*[name=sl_ref]').val();
            res[i].id = $(this).find('*[name=sl_nom_id]').val();
            res[i].desc = $(this).find('*[name=sl_desc]').val();
            res[i].vat = $(this).find('*[name=sl_vat]').val();
            res[i].net = $(this).find('*[name=sl_net]').val();
            console.log(res);
            alert(res[i].propid);

            // i++;   
            //$.each('',function( index, value ){}
            //(int)i is already incremented if you use class and not IDS

        });
        console.log(res);

        $.ajax({
            type: 'POST',
            url: '<?php echo base_url(); ?>FinancialInput/addInvoiceToLedger',
            data: res,
            sucess: function (e) {
                alert(e);
            },
            error: function (e) {
                alert(e.toString());
            }
        });

    });
});
</script>

这是在codeIgniter特定的控制器功能,请注意我已经尝试多次的东西。

This is the particular controller function in CodeIgniter, Note I have tried multiple things.

function addInvoiceToLedger(){
    // $this->load->model('SalesLedgerModel');
    // $propid = $this->input->post('propid');
    // print_r($this->input->post());
    // $date = $this->input->post('date');
    // $ref = $this->input->post('ref');
    // $nomid = $this->input->post('id');
    // $desc = $this->input->post('desc');
    // $vat = $this->input->post('vat');
    // $net = $this->input->post('net');

    $res = $this->input->post(NULL, TRUE);

    //problem is probably here, it's

    //for($x = 0; $x < count($res); $x++){


    foreach($res as $row){

    $this->SalesLedgerModel->addInvoiceToLedger($row.propid, $row.date, $row.ref, $row.nomid, $row.desc, $row.vat, $row.net);

    }
    //}


    //redirect('home/financial_input/');
}

示范功能

function addInvoiceToLedger($propid, $date, $ref, $nomid, $desc, $vat, $net){

    $data = array('sl_prop_id' => $propid, 'sl_date' => $date,
        'sl_ref' => $ref, 'sl_nominal_sub' => $nomid, 'sl_invoice_desc' => $desc, 'sl_vat' => $vat, 'sl_amount' => $net);

    $this->db->insert_batch('salesledger', $data);
}


这一切都已在他们里面每种形式的相关领域的数据。

Which all have the relevant field data for each form inside them.

我的问题是我怎么接收和处理数组?每一个循环我试过不行,我试图让变量奇(你可以看到上面的注释),但不会在$意义(或工作),因为。 AJAX数据被设置为资源,但$这个 - >输入 - >后('水库')不返回任何东西也没有$这个 - >输入 - >后(NULL,TRUE)

My question is how do I receive and handle the array? Every loop I've tried doesn't work, I've tried getting variables singularly(as you can see above commented out) but that doesn't make sense (or work) because in the $.ajax data it is set to res, but $this->input->post('res') doesn't return anything nor does $this->input->post(NULL, TRUE)

Majorly卡住了,我觉得这应该是一个简单的事情,但我显然不明白。

Majorly stuck, I feel this should be a simple thing, but I clearly don't understand it.

编辑,还是同样的问题无法找到答案的任何地方,D:,尝试了很多东西。

Edit, still same problem can't find an answer anywhere D:, tried many things.

还是一样的问题RES被作为职务,但我不能在这 - $>输入 - >后()...任何人?

Still the same problem "res" is sent as post, but I can't retrieve it in the controller with $this->input->post()...anyone?

推荐答案

我只是想提交意见,但显然我需要50代表该所以我会利用我的机会有一个答案。

I only wanted to submit a comment but apparently I need 50 rep for that so I'll take my chances with an answer.

在如果更改的数据段这是你的$就阻止什么:

In your $.ajax block what if you change the data section to this:

$.ajax({
    type: 'POST',
    url: '<?php echo base_url(); ?>FinancialInput/addInvoiceToLedger',
    data: { res : res },
    sucess: function (e) {
        alert(e);
    },
    error: function (e) {
        alert(e.toString());
    }
});

这篇关于接收和处理多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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