Jump to content

PHP Combox box getting data from database and displaying orrectly but cant selec


Recommended Posts

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>

 

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

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

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. 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.