2016-08-04 79 views
0

cart.blade.php嵌套的HTML formBuilder与Laravel

@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,成功退房运行。

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

回答

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 
+0

使用AJAX伟大的想法,但没有办法使两个操作之间的重叠? –

+0

不,但你可以检查我的答案。我认为你可以实现你想要的而不会重叠表格。 – jaysingkar

+0

如果我错了,请纠正我。答案是基于我在代码中观察到的。我已经分离了两种形式而不会失去你想要实现的功能。 – jaysingkar