I have a PHP file that fetches rows of car brands from a database, echoes these as "<option value=\"$brand\">" . $brand . "</option>";
and puts them inside pre-written <select>
tags.
My issue is that the first item that appears in the select box is not passing its value onwards.
The value of the select box is changed by this event
$('select[name=model]').on('change', function() {
selectedModel = $("#select-model").val()
});
The <option>
-tags are generated by this loop in brands.php
:
while ($row = mysqli_fetch_array($result)) {
$brand = $row['brand'];
echo "<option value=\"$brand\">" . $brand . "</option>";
}
The brands are fetched by this function:
function fetchBrands() {
$.ajax({
type: "POST",
url: "script/rent/brands.php",
data: {dateFrom: selectedDateFrom,
dateTo: selectedDateTo,
destination: selectedDestination},
success: function(data) {
$("#select-brand").html(data);
}
});
}
Because the data is posted to #select-brand
with .html()
I can't set a default value for the select box because it gets overwritten. Appending the options will result in duplicates etc. as fetchBrands()
is dependent on a previous set of radio buttons and select boxes.