brickstermike Posted December 11, 2008 Share Posted December 11, 2008 I have got a combo box on the admin page for my shopping cart which gets the selection choices from a database. It is getting them and displaying them correctly, however it wont let me select it from the list, you click on it and nothing happens. The code is below. Any help would be greatly appreciated. Many Thanks, Mike /* Generate combo box options containing the categories we have. if $catId is set then that category is selected */ function buildCategoryOptions($catId = 0) { $sql = "SELECT cat_id, cat_parent_id, cat_name FROM tbl_category ORDER BY cat_id"; $result = dbQuery($sql) or die('Cannot get Product. ' . mysql_error()); $categories = array(); while($row = dbFetchArray($result)) { list($id, $parentId, $name) = $row; if ($parentId == 0) { // we create a new array for each top level categories $categories[$id] = array('name' => $name, 'children' => array()); } else { // the child categories are put int the parent category's array $categories[$parentId]['children'][] = array('id' => $id, 'name' => $name); } } // build combo box options $list = ''; foreach ($categories as $key => $value) { $name = $value['name']; $children = $value['children']; $list .= "<optgroup label=\"$name\">"; foreach ($children as $child) { $list .= "<option value=\"{$child['id']}\""; if ($child['id'] == $catId) { $list.= " selected"; } $list .= ">{$child['name']}</option>\r\n"; } $list .= "</optgroup>"; } return $list; } The code on the actual page that uses this function; <?php if (!defined('WEB_ROOT')) { exit; } $catId = (isset($_GET['catId']) && $_GET['catId'] > 0) ? $_GET['catId'] : 0; $categoryList = buildCategoryOptions($catId); ?> <p> </p> <p> </p> <p> </p> <p> </p> <form action="processProduct.php?action=addProduct" method="post" enctype="multipart/form-data" name="frmAddProduct" id="frmAddProduct"> <table width="500" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable"> <tr><td colspan="2" id="entryTableHeader">Add Product</td></tr> <tr> <td width="150" class="label">Category</td> <td class="content"> <select name="cboCategory" id="cboCategory" class="box"> <option value="" selected>-- Choose Category --</option> <?php echo $categoryList; ?> </select></td> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/ Share on other sites More sharing options...
Adam Posted December 11, 2008 Share Posted December 11, 2008 Well what do you expect to happen? from what's visible of the form: <form action="processProduct.php?action=addProduct" method="post" enctype="multipart/form-data" name="frmAddProduct" id="frmAddProduct"> <table width="500" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable"> <tr><td colspan="2" id="entryTableHeader">Add Product</td></tr> <tr> <td width="150" class="label">Category</td> <td class="content"> <select name="cboCategory" id="cboCategory" class="box"> <option value="" selected>-- Choose Category --</option> <?php echo $categoryList; ?> </select></td> </tr> You've not got anything in place to actually cause something to happen when you select different options? I imagine you're wanting to submit the form as they change the menu? If so add: <select name="cboCategory" id="cboCategory" class="box" onchange="document.getElementById('frmAddProduct').submit();"> You could extend that using a custom JavaScript function (to add validation or whatever) and have something more like: onchange="submitAddProduct();" Then create your function like so: <script type="text/javascript"> function submitAddProduct() { // validate / do something first document.getElementById('frmAddProduct').submit(); } </script> A Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-712433 Share on other sites More sharing options...
brickstermike Posted December 11, 2008 Author Share Posted December 11, 2008 Perhaps I havent cleared myself very well. The combo box is on the add product page for a shopping cart. Basically the user can add different categories (cars, food etc) and when they go onto the add product page they can add products under the different categories. So by choosing a category from the combo list you are selecting which category you want to add the product to. Nothing is happening no hover, no selection nothing. And the above solution hasnt worked. Any further help would be appreciated. The url is http://mark-knopfler.100webspace.net/admin/product/index.php?view=add&catId=0 Thanks, Mike Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-712974 Share on other sites More sharing options...
Adam Posted December 12, 2008 Share Posted December 12, 2008 Can't picture what you mean, but tried the link and got a 404! A Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-713576 Share on other sites More sharing options...
brickstermike Posted December 12, 2008 Author Share Posted December 12, 2008 The idea is that the product being added is added to the associated category so when you open that category up the product shows on that categories page. To select the category to put the product under you use the combo box. All data is added to the category table (mysql database), however when I add a category it is showing in the combo box which would suggest that theres an error in the combobox code rather than an error with the database settings. Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-713824 Share on other sites More sharing options...
Adam Posted December 12, 2008 Share Posted December 12, 2008 Aaaaah! I see. Here's your problem: $list .= "<option value=\"{$child['id']}\""; Should be: $list .= "<option value=\"{$child['id']}\">"; A Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-713839 Share on other sites More sharing options...
brickstermike Posted December 12, 2008 Author Share Posted December 12, 2008 That doesnt seem to have worked either Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-713923 Share on other sites More sharing options...
Adam Posted December 12, 2008 Share Posted December 12, 2008 Do you have a working link? Quote Link to comment https://forums.phpfreaks.com/topic/136484-php-combox-box-getting-data-from-database-and-displaying-orrectly-but-cant-selec/#findComment-714006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.