表单数据不创建数组

时间:2016-04-08 20:28:56

标签: php arrays forms laravel

我有一个表单,我使用jQuery .cloneme来制作重复的元素。我的表单元素名称为" xxxxx []"所以他们应该在处理后创建一个数组,但我只得到第一个实例:

    @extends('app')

@section('content')

<div class="row">
    <div class="col-md-10 col-md-offset-1">
        <h1>Create a New Jobsheet</h1>
        <hr/>

        {!! Form::open(['url' => 'jobsheets']) !!}

        <div class="form-group">
            {!! Form::label('customer','Customer') !!}
            <select name="customer" size="1" class="form-control">
                    <option value="" disable selected>-- Select Customer --</option>
                @foreach($customers as $c)
                    <option value="{{ $c->name }}">{{ $c->name }}</option>
                @endforeach
            </select>
        </div>
    </div>
</div>
<br/>

<div class="row">
    <div class="col-md-5 col-md-offset-1">
        <div class="form-group">
            {!! Form::label('travel','Travel Description: ') !!}
            {!! Form::text('travel',null,['class' => 'form-control']) !!}
            {!! Form::label('travelduration','Travel Duration: ') !!}
            {!! Form::input('number','travelduration',null,['class' => 'form-control','step'=>'any']) !!}
        </div>
    </div>
    <div class="col-md-4 col-md-offset-1">
        <div class="form-group">
            {!! Form::label('mileage','Mileage: ') !!}
            {!! Form::input('number','mileage',null,['class' => 'form-control','step'=>'any']) !!}
        </div>
    </div>
</div>
<br/>

<div id="product">
    <div class="clonedInput" id="input1">
            <div class="row">
                <div class="col-md-6 col-md-offset-1">

                        {!! Form::label('product[]','Product: ') !!}
                        <select name="product[]" size="1" id="product1">
                            @foreach($products as $p)
                            <option value="{{ $p->name }}">{{ $p->name }}</option>
                            @endforeach
                        </select>
                </div>
                <div class="col-md-2 col-md-offset-1">
                        {!! Form::label('prodquant[]','Product Quantity: ') !!}
                        {!! Form::input('number','prodquant[]','1', ['class' => 'form-control', 'id' => 'prodquant1', 'step' => 'any'])
                         !!}
                </div>
            </div>
            <div class="row">
                <div class="col-md-8 col-md-offset-1">
                        {!! Form::label('proddescription[]','Description: ') !!}
                        {!! Form::textarea('proddescription[]',null,['class' => 'form-control','id' => 'proddescription1']) !!}
                </div>
            </div>
    </div>
</div>
<button class="cloneme" rel="product">Add Product</button>

        <div class="form-group">
        {!! Form::submit('Create Jobsheet', ['class' => 'btn btn-primary form-control']) !!}
        </div>

        {!! Form::close() !!}



@endsection

@section('scripts')

<script>
    //should clone and add on to the bottom of the scorer sections in the game edit form
    $(function() {
        $('.cloneme').click(function(e){
            e.preventDefault();
            var id = $(this).attr("rel");
            var clone_item = $("#" + id).find(".clonedInput");

            clone_item.clone().removeAttr("id").removeClass("clonedInput").appendTo('#'  + id);
        });
    });
</script>

@endsection

有人可以告诉我为什么当我创建副本时,表单数据不会进入数组吗?

1 个答案:

答案 0 :(得分:1)

您可以使用chrome Inspector(F12)选项卡Network调试有效的帖子数据。 在代码中,您将全部转储如下

dd($request->all());
//or
dd(\Input::all());

如果一切正常,您可以通过

循环数据
foreach($request->get('product') as $key=>$product) {
  $qty = $request->get('prodquant')[$key];
}

编辑: 它应该是:

@section('content')
{!! Form::open.... !!}
// all form inputs
{!! Form::close() !!}