2017-10-13 5 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); 


    } 

で最も苦労していますどここれはこれは私が欲しいのデータでありますクライアントテーブルからpoにフェッチするクライアント名を選択すると、請求書のテーブルが表示されます。

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

私は自分の質問を更新しました。私は入力をクライアント名から選択して、名前を選択し、クライアントデータベースからタイトルとアドレスを取り出し、入力を請求書フォームに入力するなどの入力を行いたいとします。ありがとう –

関連する問題