PHP注意未定义的偏移数组

时间:2015-08-12 06:14:36

标签: php

我在表格行中有一个输入表单。我正在使用数组将文本输入值传递给变量,以防用户添加其他行。但是当提交包含多行的表单时,我会得到一个未定义的偏移通知。

我有一个js函数,顺便添加表行。

HTML

<div class="pull-right">
                    <form action="" method="POST"enctype="multipart/form-data">
                    <input type="text" hidden="true" readonly="true" id="prod_rowcount" name="prod_rowcount">
                    <button type="button" class="btn btn-primary btn-xs" id="prod_add_row"><span class="glyphicon glyphicon-plus"></span>&nbsp;Row</button>
                    <button type="button" class="btn btn-primary btn-xs" id="prod_remove_row"><span class="glyphicon glyphicon-minus"></span>&nbsp;Row</button>
                </div>
                    <div class="table-responsive" style="margin-top: 50px;">
                        <table class="table table-bordered" id="prod_master_table">
                            <thead>
                                <th>Name</th>
                                <th>Category</th>
                                <th>Description</th>
                            </thead>
                            <tr id="prod_master_table_row">
                                <td><input type="text" class="form-control" name="prod_name[]" required></td>
                                <td><input type="text" class="form-control" name="prod_category[]" required></td>
                                <td><input type="text" class="form-control" name="prod_description[]"></td>
                            </tr>
                        </table>
                    </div>

PHP

if(isset($_POST['btn_add_product'])){
    $rowcount = $_POST['prod_rowcount'];
    for($x=1; $x<=$rowcount; $x++){
        $prod_array_index = $x-1;
        $prod_name = $_POST['prod_name'][$prod_array_index];
        $prod_category = $_POST['prod_category'][$prod_array_index];
        $prod_description = $_POST['prod_description'][$prod_array_index];
    }

JS

$(document).ready(function(){

var prod_rowcount = 1;

document.getElementById('prod_rowcount').value = prod_rowcount;

//adding rows
$('#prod_add_row').click(function(){ 
    $('#prod_master_table').append('<tr id="prod_master_table_row">\
            <td><input type="text" class="form-control" name="prod_name[]"></td>\
            <td><input type="text" class="form-control" name="prod_category[]"></td>\
            <td><input type="file" name="prod_img[]" id="prod_img"></td>\
            <td><input type="text" class="form-control" name="prod_description[]"></td>\
        </tr>');
    prod_rowcount++;
    document.getElementById('prod_rowcount').value = prod_rowcount;

$('#prod_remove_row').click(function(){
    if(prod_rowcount>1){
        $('#prod_master_table_row:last').remove();
    }
    else{
        /*do nothing*/
    }

    prod_rowcount--;

    if(prod_rowcount==0){
        prod_rowcount = 1;
    }
    document.getElementById('prod_rowcount').value = prod_rowcount;
});
});

我收到此错误

Notice: Undefined offset: 1 in C:\xampp\htdocs\xampp\wci-jpms_version_4\index.php on line 9

Notice: Undefined offset: 1 in C:\xampp\htdocs\xampp\wci-jpms_version_4\index.php on line 10

Notice: Undefined offset: 2 in C:\xampp\htdocs\xampp\wci-jpms_version_4\index.php on line 8

Notice: Undefined offset: 2 in C:\xampp\htdocs\xampp\wci-jpms_version_4\index.php on line 9

Notice: Undefined offset: 2 in C:\xampp\htdocs\xampp\wci-jpms_version_4\index.php on line 10

任何解决方案?

2 个答案:

答案 0 :(得分:0)

您不需要行数。您可以将foreach用作:

if(isset($_POST['btn_add_product'])){
    foreach ($_POST['prod_name'] as $key => $productName) {
        $productCategory = $_POST['prod_category'][$key];
        $productDescription = $_POST['prod_description'][$key];
        // now you have available $productName, $productCategory and $productDescription.
        // when updating you can add an hidden input to know the id of the product
    }
}

答案 1 :(得分:0)

请试试这个

if(isset($_POST['btn_add_product'])){
        $prodNameArr = $_POST['prod_name']; // You can use any of the fields which is manadatory
        foreach($prodNameArr as $prod_array_index => $prodName){
            $prod_name = $prodName;
            $prod_category = $_POST['prod_category'][$prod_array_index];
            $prod_description = $_POST['prod_description'][$prod_array_index];
        }
    }