2017-05-31 46 views
0

我需要將AJAX變量傳遞到我的視圖中,以便form action正確執行。使用Laravel 5.4。和我(略)觀點是這樣的:如何將JS變量應用於Laravel表單操作

<div id="product_form" class="form-horizontal"> 

    {{ Form::open(['action' => '[email protected]_product', // PRODUCT_ID MUST GO HERE]) }} 


    <div class=" form-group"> 
     <div class="col-sm-2 col-md-2">{{ Form::label('product_name', 'Product Name:') }}</div> 
     <div class="col-sm-4 col-md-4">{{ Form::text('product_name',null) }}</div> 
     <div class="col-sm-2 col-md-2">{{ Form::label('product_id', 'Product Code') }}</div> 
     <div class="col-sm-4 col-md-4">{{ Form::text('product_id', null) }}</div> 
    </div> 

我的Ajax功能正確傳遞所有的值到表單字段,但是我很努力讓product_id成在操作語句的正確位置。 Laravel期望看到一個變量。

Ajax代碼(略):

select: function(event, ui) { 
     var product_id = (ui.item.value); 

     $.ajax({ 
      url: "product_fullsearch/"+product_id, 
      dataType: "json", 
      type:"GET", 
      success: function (data) { 
       $('#product_name').val(data[0].product_name); 
       $('#product_id').val(data[0].product_id); 
       $("#supplier_name").val(data[0].supplier_name); 
       $("#supplier_id").val(data[0].supplier_id); 
      } 
     });//END AJAX 

    } 

我看到這個Pass JS variable to form action對SO,給它一去,但我不能讓它在Laravel方面的工作。

我什麼東錯了?非常感謝 !

回答

0

我認爲解決這個問題的另一種方式:

試圖將一個變量傳遞到form action相反的,我改變了路線,使得它不再需要一個id。然後在控制器中我只是拉從$request陣列id值是這樣的:

$id = $request->product_id;

我這樣的更新工作:

$id = $request->product_id; 

    Product::updateOrCreate(array('product_id' => $id), 
     array(
      'product_name' => $request->product_name, 
      'product_id'=>$id, 
     )); 

我希望這可以幫助別人。

相關問題