I need to reload this HTML dropdown list after some database details get changed. Any ideas?
Note: ddl_my_items.reload();
is not working.
Thanks in advance.
<?php
//populates the ddl of items
$items = $dal->get_items() or die(mysql_error());
echo "<select id='ddl_my_items'>";
echo "<option value=''></option>";
foreach ($items as $item){
echo "<option value='$item->item_name'>$item->item_name</option>";
}
echo "</select>";
?>
It's easiest if you include jQuery.
create a php file ajax.php that only echoes the dropdown list contents:
<?php
//populates the ddl of items
$items = $dal->get_items() or die(mysql_error());
echo "<option value=''></option>";
foreach ($items as $item){
echo "<option value='$item->item_name'>$item->item_name</option>";
}
Then, use the following javascript code:
$.get('ajax.php', function(result) {
$('#ddl_my_items').html(result);
}
The function $.get
sends an AJAX request (which is simply a HTTP request whose response can be received in javascript). The anonymous function is called as soon as the contents have been received.
$('#ddl_my_items')
returns a jQuery object of the select
element, and its html
allow you to alter the element's content by passing the html code you've just received by the ajax call.