与Laravel嵌套的html formBuilder

时间:2016-08-04 20:44:55

标签: laravel formbuilder

cart.blade.php

@extends('master')

@section('content')
    <div class="container">
        @if(Cart::isEmpty())
            <b>The card is empty</b> <a href="/products">Shopping now</a>
        @else
            <table class="table table-condensed table-bordered">
                <thead>
                    <tr>
                        <th>item id</th>
                        ....
                    </tr>
                </thead>
                <tbody>
                    {!! Form::open(["url"=>"/pay"]) !!}
                    <?php $i = 0; $totalCart_price = 0; ?>
                    @foreach($cart_total_items as $item)
                        <tr>
                            <td>{{ $item['id'] }}</td>
                            ....
                            <td>
                                {!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
                                    <button type="submit" class="btn btn-danger" aria-label="Left Align">
                                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                    </button>
                                {!! Form::close() !!}
                            </td>
                        </tr>
                        <?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
                    @endforeach         
                </tbody>
            </table>
            <b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
            <br><br>
            {!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
            {!! Form::close() !!}
            <a href="/my-cart/clear" class="btn btn-danger">Clear cart</a>
        @endif
    </div>
@stop

问题:我可以删除任何项目,但在点击检查期间没有任何反应,但是当我删除clear item form时,请检查运行是否成功。

我想运行两个操作,我该如何解决?
感谢

1 个答案:

答案 0 :(得分:0)

您可以在页面中包含多个表单,但不应嵌套它们 正如html5 working draft中所述:

  

4.10.3表单元素

     

内容模型:

     

流内容,但没有表单元素后代。

据我所知,form只是html标签,可用于一次向服务器发送一组数据(数组)。
在你的情况下,你最好的选择是使用ajax查询。(虽然它会使我们的页面javascript依赖)
或者,正如我所观察到的,你可以将下面的两个形式分开:

@extends('master')

@section('content')
    <div class="container">
        @if(Cart::isEmpty())
            <b>The card is empty</b> <a href="/products">Shopping now</a>
        @else
            <table class="table table-condensed table-bordered">
                <thead>
                    <tr>
                        <th>item id</th>
                        ....
                    </tr>
                </thead>
                <tbody>
                    <<!-- Remove the Form tag from here and insert it below -->>
                    <?php $i = 0; $totalCart_price = 0; ?>
                    @foreach($cart_total_items as $item)
                        <tr>
                            <td>{{ $item['id'] }}</td>
                            ....
                            <td>
                                {!! Form::open(["url"=>"/my-cart/".$item['id'], "method"=>"DELETE", "style"=>"display: inline"]) !!}
                                    <button type="submit" class="btn btn-danger" aria-label="Left Align">
                                        <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>
                                    </button>
                                {!! Form::close() !!}
                            </td>
                        </tr>
                        <?php $i++; $totalCart_price += Cart::get($item['id'])->getPriceSum(); ?>
                    @endforeach         
                </tbody>
            </table>
            <<!-- Remove the Form tag from above and insert it below -->>
            {!! Form::open(["url"=>"/pay"]) !!}
            <b>Total price: ${{ $totalCart_price }} {{ Form::hidden("total_price", $totalCart_price) }}</b>
            <br><br>
            {!! Form::submit('check out (paypal)', ["class"=>"btn btn-primary"]) !!}
            {!! Form::close() !!}
            <a href="/my-cart/clear" class="btn btn-danger">Clear cart</a>
        @endif
    </div>
@stop