So, I have form as shown in the code snippet below.
tl;dr - form has a select field that gets its options dynamically from database table with a distinct sql query and on submit inserts new row into the table, then returns to the form.
Issue - after submit and insert, the drop down select field doesn't show the newest added option even though the row was added to the table. I have to manually refresh the page to get the drop down to have the newest option available.
Please help me with this issue as I do not want to load this page on each time new data item is added.
How can I force the form to re-populate the select field after each successful submit?
<form method="POST" action="">
<?php
$lsql = 'SELECT distinct(location) FROM some_table';
$result = mysql_query($lsql);
?>
<select class="field" name="locations" onchange="showfield(this.options[this.selectedIndex].value)">
<?php while($row = mysql_fetch_array($result)) {
$key = str_replace(',', '', $row['location']);
$key = str_replace(' ', '', $key);
$loc = $row['location'];
$locations[$key] = $loc;
echo '<option value="'.$key.'">'.$loc.'</option>';
}
?>
<option value="other">Other</option>
</select>
<div id="otherloc"></div>
<?php
if (isset($_POST['newjob'])) {
// getting form data
$location = $_POST['location'];
$other = $_POST['other'];
$jlocation = ($locations[$jlocation])? $locations[$jlocation] : $jother;
// Then, get all the fields data and insert into a database table
header('Location: ' . $_SERVER['PHP_SELF']);
}
?>
The showfield() is a javascript function that adds an input text field in the 'otherloc' div when the 'Other' option is selected in the dropdown. The insert changes the same table, so I need to get fresh distinct(locations) every time the form loads anew.