2016-08-04 95 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