I have a field called price in the plans table.
public function up(): void
{
Schema::create('plans', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->foreignId('doctor_id')->constrained()->cascadeOnDelete();
$table->foreignId('type_id')->constrained()->cascadeOnDelete();
$table->string('price')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
}
And I have two tags, select option
and span
.
<select class="form-select" id="plan_id" name="plan_id">
@foreach($plans as $plan)
<option value="{{ $plan->id }}">{{ $plan->title }}</option>
@endforeach
</select>
<span id="totalPrice"> </span>
I want to display the totalPrice
id price after the select option is selected. Which shows undefined
.
<script>
document.addEventListener('DOMContentLoaded', function() {
var planSelect = document.getElementById('plan_id');
var timeSelect = document.getElementById('time_id');
var priceTotal = document.getElementById('totalPrice');
planSelect.addEventListener('change', function() {
var planId = this.value;
var xhr = new XMLHttpRequest();
xhr.open('GET', '{{ route('findIDTime') }}?id=' + planId, true);
xhr.onload = function() {
if (xhr.status === 200) {
var data = JSON.parse(xhr.responseText);
console.log(data);
timeSelect.innerHTML = '';
for (var i = 0; i < data.length; i++) {
var option = document.createElement('option');
option.value = data[i].id;
if (data[i].direction === 1) {
option.textContent = data[i].start_time + ' صبح';
} else {
option.textContent = data[i].start_time + ' عصر';
}
timeSelect.appendChild(option);
priceTotal.innerText = data[i].price;
}
} else {
console.log('Error: ' + xhr.status);
}
};
xhr.onerror = function() {
console.log('Request failed');
};
xhr.send();
});
planSelect.dispatchEvent(new Event('change'));
});
</script>
web.php
Route::get('/findIDTime', [AppHttpControllersHomeController::class, 'findIDTime'])->name('findIDTime');
HomeController.php
public function findIDTime(Request $request)
{
$data = Time::query()->where('plan_id', $request->id)->get();
return $data;
}
2
Answers
To understand what you mean, if you have already set the plan price in the database table, and what you want is the price to be displayed after choosing one of the plans through selection, right?
You can try this method, it may help in solving the problem
In the control, add this code
JS code
Based on a comment in my first answer "I get this error Uncaught TypeError: data[0] is undefined" 👇
The error you encountered "TypeError: Data[0] is undefined" indicates that an attempt to access data[0] in your code occurs when the data is undefined or empty.
In this case, it looks like you are assigning the value to PriceTotal.innerText using data[0].price, but it may happen that the data is not an array or the first element in the array is not defined.
To avoid this error, you can add an additional check to ensure that the data is not empty and contains elements before attempting to access the data[0]. You can check this by adding a condition before using data[0] like so:
Here’s the part of your code that needs modification:
This will set the
innerText
of thepriceTotal
span to the price of the first item in thedata
array.However, if you have multiple times associated with a plan and you want to display the total price of all those times, you might want to calculate the total price separately.
Here’s how you can modify your JavaScript code to calculate the total price and display it:
This code uses the reduce function to sum up all the prices in the
data
array. It then sets theinnerText
ofpriceTotal
to the total price.Additionally, you might want to format the price for better readability. I added
toFixed(2)
to round the total price to two decimal places. Adjust this based on your needs.Now, this modification should correctly display the total price in the
priceTotal
span after a select option is chosen.