0

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.

Sagar Awasthi
  • 538
  • 5
  • 10
amrita
  • 83
  • 4
  • hmmm i think this already happens by doing `header('Location:...` after inserting. Does your `header('...` work? Another way is to do the insert before the select but the header stuff is better because of various reasons. – steven May 19 '14 at 12:09
  • Uuuups header could not work because of doing some outputs before sending header. Do the `if (isset($_POST['newjob'])) { ...` before your outputs. – steven May 19 '14 at 12:12
  • @steven - the header works fine, i have tried various pages in the directory structure and it redirects correctly. – amrita May 20 '14 at 04:42
  • 1
    I put the isset part up top as @nothing reminded me and it works fine now. Thanks guys. I knew there is a reason why I had always done so but for whatever reason, I put it down below this once and couldn't see the obvious flaw. – amrita May 20 '14 at 04:54

2 Answers2

1

Check your error logs to see if everything is ok. I suspect that header(...) is not working. Also you might consider inserting location to database before you create the select.

Marius.C
  • 700
  • 6
  • 14
0

You should write your queries like this or revert the parts like
<?php if (isset($_POST['newjob'])) { } ?> <form method="POST" action=""> </form>
I think it will help you

  • here is the something like urs asked by me http://stackoverflow.com/questions/23669287/fetch-altered-data-after-page-is-updated –  May 19 '14 at 12:27
  • 1
    It works but I put that part right up top after the session_start() . I prefer it that way too as I don't like to put it somewhere within the body. It messes with formatting and makes scrolling to other parts of the html annoying. Thanks though. – amrita May 20 '14 at 05:00
  • If i am clear then you can put if statement before form where you want to display errors . And you can put this anywher in body before the form it will not harm you anymore or if you put in header then it may distract your design –  May 20 '14 at 08:37