I currently have an auto populating select box depending on what option you select in the first select box. This is all working fine and does the job.
What i am trying to achieve is when the form is submitted it retains the values selected. I can get this working for the first initial select box, but when it comes to the second select box (the one loaded automatically) i cant seem to get it working.
The first select box id is ctlJob with the static information, and the second one has an id of ctlPerson.
Here is my HTML:
<form id="homesearch" action="view.php" method="get">
<label for="ctlJob"></label>
<select name="id" id="ctlJob" style="clear:both; float:left;">
<option value="0">Please Select...</option>
<option <?php if ($_GET['id'] == '1') { ?>selected="true" <?php }; ?>value="1">Regional General Manager</option>
<option <?php if ($_GET['id'] == '2') { ?>selected="true" <?php }; ?>value="2">General Manager</option>
<option <?php if ($_GET['id'] == '3') { ?>selected="true" <?php }; ?>value="3">Deputy General Manager</option>
<option <?php if ($_GET['id'] == '4') { ?>selected="true" <?php }; ?>value="4">Operations Manager</option>
<option <?php if ($_GET['id'] == '5') { ?>selected="true" <?php }; ?>value="5">Shift Manager</option>
<option <?php if ($_GET['id'] == '6') { ?>selected="true" <?php }; ?>value="6">First Line Manager</option>
<option <?php if ($_GET['id'] == '7') { ?>selected="true" <?php }; ?>value="7">Stock Controller</option>
<noscript>
<input type="submit" name="action" value="Load Individuals" />
</noscript>
<select name="person_id" id="ctlPerson" style="clear:both; float:left;">
</select>
<input id="submit" type="submit" name="action" value="search" style="margin-top:40px;" />
</form>
Here is my js:
<script type="text/javascript">
document.getElementById('id').value = "<?php echo $_GET['id'];?>";
document.getElementById('person_id').value = "<?php echo $_GET['person_id'];?>";
</script>
Any ideas on how i can get the secondry select box to stay populated with the option selected after the form has been submitted to itself?
Thanks, Dan
Sorry i missed this js out:
<script type="text/javascript" charset="utf-8">
$(function(){
$("select#ctlJob").change(function(){
$.getJSON("view.php",{id: $(this).val(), ajax: 'true'}, function(data){
var $persons = $("#ctlPerson").empty();
$.each(data, function() {
$persons.append("<option value=" + this.optionValue + ">" + this.optionDisplay + "</option>");
});
})
});
})
</script>
Whatever code you use to populate it (presumably a change
event handler), just call it on page load. I do stuff like that with this kind of code:
(elem.onsomething = function() {
// some event-handling code
}).call(elem);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.