2017-10-13 60 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

我已更新我的问题。我想根据选择的名称填充输入,例如,我选择客户端名称,我想从客户端数据库获取标题和地址,然后在发票表单中填充输入。谢谢 –

相关问题