Created
September 27, 2017 09:21
-
-
Save mmuoDev/f02acb02a1a0017e7aec6ddc839261a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For those with Javascript phobia like me, this is how to dynamically load contents into a input textfield using a select box. This gist uses AJAX and JQuery. | |
The view | |
<div class="form-group col-sm-8"> | |
<label class="">Select Bank Account</label> | |
<select class="form-control bank_account" name="bank_account"> | |
<option value="">--Please select--</option> | |
@foreach($banks as $bank) | |
<option value="{{$bank->id}}">{{$bank->name}}[{{$bank->account_number}}]</option> | |
@endforeach | |
</select> | |
</div> | |
<div class="form-group col-sm-8"> | |
<label class="">Account Balance</label> | |
<input name="balance" id="balance" type="number" class="form-control balance" readonly="true"/> | |
</div> | |
<-- Include this script in your view --> | |
<script> | |
$('.bank_account').on('change', function(e){ | |
var token = $("input[name='_token']").val(); | |
var bank_account = e.target.value; | |
//ajax | |
$.ajax({ | |
url: "<?php echo route('get-balance') ?>", | |
method: 'POST', | |
data: {bank_account:bank_account, _token:token}, | |
success: function(data) { | |
console.log(data); | |
$('#balance').empty(); | |
$('#balance').val(data[0].currentBalance); | |
} | |
}); | |
}); | |
</script> | |
Your route file | |
Route::post('get-balance', 'FinanceController@get_balance')->name('get-balance'); | |
Your Controller | |
public function get_balance(Request $request){ | |
$account_number = $request->bank_account; | |
$balance = DB::select("select currentBalance from sys_bank_accounts where id = '$account_number'"); | |
return response()->json($balance); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment