I have a select list in my form like
<form class="form-horizontal form-bordered" method="post" action="formaction">
<div class="form-group">
<label class="col-md-3 control-label" for="inputSuccess">Amount</label>
<div class="col-md-6">
<select class="form-control mb-md" name="amount" id="amount">
<option value="10"> 10</option>
<option value="20">20</option>
<option value="30">30</option>
<option value="40">40</option>
<option value="50">50</option>
</select>
</div>
<div class="input-group mb-md">
<button type="submit" class="btn btn-warning btn-sm">Submit</button>
</div>
</form>
<script>
$("#sub").click(function(){
$.ajaxSetup({
headers: { 'X-CSRF-Token' : $('meta[name=_token]').attr('content') }
});
var selectedVal= $("select option:selected").val();
return $.ajax({
type: 'GET',
url: 'getdollarvalue',
dataType: 'json',
contentType: "application/json; charset=utf-8",
data: {
"value": selectedVal
},
success: function(response){
return alert(response);
}
});
});
</script>
And I have a table in database that stores corresponding values to selected dropdown values like
id value conversion
1 10 0.2
2 20 0.32
3 30 0.43
4 40 0.77
and so on
my routes.php
Route::get('getdollarvalue/{value}', [
'as' => 'getdollarvalue', 'uses' => 'dashboardController@getResult'
]);
and my controller
public function getResult(){
$result = Input::get('value');
return $result;
}
Now, When I submit my form, I want to show the users in alert the corresponding value of the selected one. For example, If user selects 20 from the dropdown, then, on submitting the form, users is supposed to see the corresponding conversion of 20 i.e 0.32 in this case in alert. I know I have to implement ajax for this, I just don't understand how to implement it. Kindly guide me through this one.