How Can I Refresh A Detail Select List When My Master Select Changes Using Ajax
I'm looking for some pointers. I have one select list with a list of topics;
Solution 2:
I asked a similar question and it got closed because it was a a duplicate of this apparently. Here is what I asked Using PHP/SQL - How can I click an item in a list to display another list of items assoicated to the clicked item I use a list of links rather than a drop down select.
Anyways I got it working with some help.
I have 2 tables. the "list" table (which has ID and listName fields) and a "listitems" table (which has id,itemName, and parent fields). When you click a list the items that have that list as a parent are displayed in another list next to it.
Here you go. It has deprecated functions and is sloppy but it wil do for now while I learn more. I'm still a beginner but if I get around to cleaning it up I will edit this.
<?phprequire ('./includes/connection.inc.php');
/* AJAX request */if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{
$query = sprintf('SELECT * FROM listitems WHERE parent=%d',
mysql_real_escape_string($_REQUEST['parent']));
$listitems = mysql_query($query)
ordie(mysql_error());
printf('<h3>List %d Items</h3>', $_REQUEST['parent']);
while($info = mysql_fetch_array( $listitems ))
{
echo$info['itemName']."<br />";
}
exit;
}
/* Normal request */?><divid="lists"><h3>Lists</h3><?php$lists = mysql_query("SELECT * FROM lists")
ordie(mysql_error());
while($info = mysql_fetch_array( $lists ))
{
echo'<a href="?parent='.$info['id'].'">'.$info['listName'].'</a><br />';
}
?></div><divid='listitems'><?php$query = sprintf('SELECT * FROM listitems WHERE parent=%d',
mysql_real_escape_string($_REQUEST['parent']));
$listitems = mysql_query($query)
ordie(mysql_error());
printf('<h3>List %d Items</h3>', $_REQUEST['parent']);
while($info = mysql_fetch_array( $listitems ))
{
echo$info['itemName']."<br />";
}
?></div><scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><script>jQuery(function($)){
$('#lists').delegate('a', 'click', function(e){
e.preventDefault();
$('#listitems').load($(this).attr('href'))
});
}
</script>
Post a Comment for "How Can I Refresh A Detail Select List When My Master Select Changes Using Ajax"