I use an Ajax call for the select option which loads when I change the select option. I want the first option to load when I load the page without selecting. I'm using the W3schools to achieve this (https://www.w3schools.com/php/php_ajax_database.asp).
My (W3 schools) working code for this:
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
<select id="gang" class="form-control" name="users" onchange="showUser(this.value)" style="height: auto;">
<?php
$sql = "SELECT DISTINCT id, datum FROM analyse ORDER BY date DESC";
$result = mysqli_query($conn, $sql) or die ("Connection failed: " . $conn->connect_error);
while ($row = mysqli_fetch_assoc($result)) {
$id = $row['id'];
?>
<option value="<?= $id; ?>"><?= $id; ?></option>
<?php } ?>
</select>
Fix was to onload on body and add an id to the option field:
<body onload="showUser(document.getElementById('analyse').value)">
<option id="analyse" value="<?= $id; ?>"><?= $id; ?></option>