Hi,
I'm loading multiple items from a database using php. Each item has a 'category' attribute. I'm trying to run a jquery function, which loads a php script, for each iteration of an item. The script fetches all category options from the databse and populates a <select> object.
The reason for this roundabout method is that there is an option at the top of the page to add a new category. If a new category is added, I want to reload the above function for every item, so the new category is added to the <select>.
- My php script 'loadCats.php' has the following:
- select id="category" name="category">
- <option value="">Select Category</option>
- <?
- while ($row = mysql_fetch_array($sql))
- {
- $val = $row['category'];
- if ($cat != "")
- $selected = $cat;
- else
- $selected = $_SESSION['category'];
- ?>
- <option value= "<? print $val ?>" <? if($selected == $val) print "selected" ?> ><? print $val; ?></option>
- <?
- }
- ?>
- </select>
the jquery function calling loadCats.php is as follows:
- <td align="left" valign="middle" id="displayCats">
- <script>
- $("#displayCats").each(function() {
- loadCats();
- });
- function loadCats()
- {
-
- $('#displayCats').load('loadCats.php',{"cat":"<? print $row['category'] ?>"});
- };
- });
- </script>
- </td>
Only the last item has a drop down menu. The rest of the items have nothing.
Thanks