【问题标题】:Receiving and Processing a multi dimensional array接收和处理多维数组
【发布时间】:2014-04-04 16:55:59
【问题描述】:

我已经问了两次这个问题,没有一个满意的答案,也就是说,我之前遇到的两个问题仍然存在,我将如何继续这个问题。

这次我将提供所有代码和我收到的内容,以及问题所在,我知道问题出在哪里,但由于我对使用 AJAX 比较陌生,这让我感到困惑。

我有一个表单,当您单击倒数第二个字段时,它会自我复制,在另一个用户的帮助下,所有 id 都被更改为唯一。

<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

<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 中特定的控制器功能,注意我已经尝试了多种方法。

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

它们里面都有每个表单的相关字段数据。

我的问题是如何接收和处理数组?我尝试过的每个循环都不起作用,我尝试过单独获取变量(正如您在上面所看到的那样),但这没有意义(或工作),因为在 $. ajax 数据它设置为 res,但 $this->input->post('res') 不返回任何内容,$this->input->post(NULL, TRUE)

严重卡住了,感觉这应该是一件简单的事情,但我明显看不懂。

编辑,同样的问题在任何地方都找不到答案 D:,尝试了很多东西。

同样的问题“res”作为 post 发送,但我无法在控制器中使用 $this->input->post()...anyone 检索它?

【问题讨论】:

    标签: php ajax arrays codeigniter


    【解决方案1】:

    我只想提交评论,但显然我需要 50 个代表,所以我会抓住机会回答。

    在您的 $.ajax 块中,如果您将数据部分更改为:

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

    【讨论】:

    • 应该是这样的,但是,不是让我检查是不是问题,如果解决了,我先谢谢你,然后去打自己哈哈。
    • 它仍然在 foreach 循环中返回无效参数,也就是 res 没有设置任何东西。
    猜你喜欢
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 1970-01-01
    • 2014-09-27
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多