2017-10-13 57 views
1

這是我的發票形式。我想根據來自數據庫選項字段的選擇填充標題。如何從客戶數據庫獲取值以填充發票表單中的標題。謝謝。如何根據另一個輸入所做的選擇來填充輸入

<form action="{{ route('admin.invoices.store') }}" method="post"> 

    {{ csrf_field() }} 

    <div class="row"> 

      <div class="col-md-3"> 
       <div class="form-group"> 

       <label for="invoicenumber">Invoice Number</label><br> 
       <input class="form-control" type="text" name="invoice_no" id="number"> 
       <span class="alert-danger" id="number-feedback"></span> 


       </div> 
       <div class="form-group"> 
       <label for="client">Client</label><br> 
       <select name="client" id="client_id" class="form-control"> 
        <option value="select">Select Client</option> 
        @foreach($clients as $client) 
         <option id="option" value="{{ $client-> id|$client->name }}">{{ $client->name }}</option> 
        @endforeach 
       </select> 


       </div> 

       <div class="form-group"> 
        <label for="Title">Title</label><br> 
       <input class="form-control" type="text" name="title" id="title"> 
       <span class="alert-danger" id="title-feedback"></span> 
       </div> 
      </div> 

我的路線

Route::post('/populate', '[email protected]'); 

我的Ajax請求

$(document).on('change', '#client_id', function(event){ 

    var id = $("#client_id").find(":selected").text(); 



$.ajax({ 
     type: "POST", 
     url: '/populate', 
     data: {id: id, '_token':$('input[name=_token]').val()}, 
     success: function(data) { 
      //$('#refresh').load(location.href + ' #refresh'); 
      console.log(data); 
     } 
    }); 

}); 

的這是我很努力最有邏輯

public function populate(Request $request){ 

    $client =Client::find($request->id); 


    } 

這就是我想要的數據從客戶端表中取回到寶當我選擇客戶名稱時計費發票表。

Schema::create('clients', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('company'); 
     $table->string('title'); 
     $table->string('name'); 
     $table->string('phone_number'); 
     $table->string('email'); 
     $table->string('address'); 
     $table->string('state'); 
     $table->string('city'); 
     $table->string('country'); 
     $table->string('code'); 
     $table->string('info'); 
     $table->string('image'); 
     $table->timestamps(); 
    }); 

回答

0
I finally got it right results: 
after getting id: 

$client = Client::find($request->id); 

    return $client; 
I display data in ajax call, where I was struggling so it worked fine 

$(document).on('change', '#client_id', function(event){ 

    var id = $("#client_id").val(); 



$.ajax({ 
     type: "POST", 
     url: '/populate', 
     data: {id: id, '_token':$('input[name=_token]').val()}, 
     success: function(data) { 

      $("#title").val(data.title); 
      $("#address").val([ 

       data.address, 
       data.state, 
       data.city, 
       data.country, 
       data.code 


      ]); 
      //$('#refresh').load(location.href + ' #refresh'); 
      console.log(data.title); 
     } 
    }); 

}); 
0

執行以下操作:

<select name="client" id="client_id" class="form-control"> 
        <option value="select">Select Client</option> 
        @foreach($clients as $client) 
         <option id="option" value="{{ $client->id}}">{{ $client->name }}</option>//set the value to the client id 
        @endforeach 
       </select> 

JS:

var id = $("#client_id").val();//get the value 

不要忘了返回客戶端在你的控制器功能

+0

我得到的價值從id,當我登錄控制檯時,我不需要做出改變$ request-> all()返回選定的值,但我不知道如何填充輸入標題,這是我掙扎的地方。謝謝 –

+0

輸入什麼標題?你想填充什麼? – madalinivascu

+0

我已更新我的問題。我想根據選擇的名稱填充輸入,例如,我選擇客戶端名稱,我想從客戶端數據庫獲取標題和地址,然後在發票表單中填充輸入。謝謝 –

相關問題