I have an HTML form which selects towns from a MYSql Db and through PHP "foreach" returns an array of results, which are then seen in the dropdown. I would like to then be able to click on one of the returned results within the dropdown and use it to populate a table further on in the code as a PHP variable (or another way if possible). I have attached the code. The dropdown works, but the town that is clicked on does not return the expected result, but rather always returns the last result in the list, making me think that the JS portion of my code is incorrect. I have very limited knowledge of JS, so I may very well be approaching this totally incorrectly. Any input appreciated.
<div class="container">
<div class="table-responsive">
<table class="table table-hover tablewd">
<thead>
<tr>
<th>Origin Town</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<form>
<div class="form-group">
<select class="form-control" id="colltown" name="colltown">
<?php {
$sql1 = "SELECT * FROM town_info";
$sth1 = $pdo->prepare($sql1);
$sth1->execute();
$towns = $sth1->fetchAll(PDO::FETCH_ASSOC);
foreach ($towns as $town) {
echo "<option value='$town[id]'>$town[place_name]</option>";
}
}?>
<script>
function myFunction() {
document.getElementById("colltown").value;
}
</script>
<button onclick="myFunction()">
<?php echo $origin_town = $town[place_name];?>
</button>
</select>
</div>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>