I have a select that gets the values from my database table. There are five prices in the amount selection, and the prices selected will have different cut-off years. What I want to do is when I select the amount, and then select the start date after that, the deadline will come out automatically based on the table column years to count. What should I write in my Javascript?
This is my submit form
<form id="guanyinForm">
<div class="form-group row mb-2">
<label for="sx_ID" class="col-sm-4 col-form-label">sxID:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="sx_ID" value="{{ $light->sx_ID }}" readonly>
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_receipt" class="col-sm-4 col-form-label">Receipt Number:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_receipt">
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_amount" class="col-sm-4 col-form-label">Amount:</label>
<div class="col-sm-8">
<select name="gylight_amount" id="gylight_amount" class="form-control">
<option>Choose amount</option>
@foreach ($gyprice as $item)
<option value="{{ $item->gylight_amount }}">
RM{{ $item->gylight_amount }}
</option>
@endforeach
</select>
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_number" class="col-sm-4 col-form-label">Light Numer:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_number">
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_label" class="col-sm-4 col-form-label">Label:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_label">
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_remark" class="col-sm-4 col-form-label">Remark:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_remark">
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_remark" class="col-sm-4 col-form-label">Start Date:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_sDate">
</div>
</div>
<div class="form-group row mb-2">
<label for="gylight_remark" class="col-sm-4 col-form-label">Due Date:</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="gylight_eDate">
</div>
</div>
<div class="row">
<div class="col-12">
<button class="btn btn-primary float-right" id="guanyinAdd">Submit</button>
</div>
</div>
</form>
This is my table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateGypricesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('gyprices', function (Blueprint $table) {
$table->bigIncrements('id');
$table->double('gylight_amount',8,2)->comment('price of the light');
$table->bigInteger('years')->comment('how many years');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('gyprices');
}
}
This is my controller
public function view(Light $light){
$gylight = Guanyin::latest()->first()->orderBy('id','desc')->get();
$gyprice = Gyprice::latest()->first()->orderBy('id','asc')->get();
return view('admin.light.view',[
'light' => $light,
'gylight' => $gylight,
'gyprice' =>$gyprice,
]);
}
This is my script
<script>
$('#gylight_amount').on('change', function() {
var year = $(this).find(':selected').data('years');
var amount = $('#gylight_amount').val();
var startDate = $('#gylight_sDate').val();
var date = new Date(startDate);
var yyyy = date.getFullYear() + year;
var m = date.getMonth();
var d = date.getDate();
$("#gylight_eDate").val(d + "/" + m + "/" + yyyy);
});
</script>