I'm using code from this site http://www.phpwebcommerce.com/index.php I just want to get better at PHP and learn it so I set up a simple store following that tutorial. However in the admin section when I try to add a new product the combo box doesn't allow me to select the category. It is listing the category, but it isn't allowing me to select it
This is the web page that refers to this part I am stuck on: http://www.phpwebcommerce.com/shop-a...dd-product.php
Below is the code that I am using:
PHP Code:
/*
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());
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);
}
}
I don't know if this will help but at the end of </option> you have "rn" and the link you posted has "\r\n". It also looks like there is no echo of a "<select>" if that is the case you won't get a drop down menu. What exactly are you seeing since just saying "it won't let me select" isn't clear enough?
Thanks for replying sorry I wasn't being clear. I get a drop down menu which does contain the value that I want to select. However when i click on it, it doesn't actually select it, it just goes back to the default setting. For example it has a box that says:
-- Choose Category -- and it has an arrow by it. You click on that and the 'music' category drops down. But when i click on music the drop down menu just disappears and instead of the --Choose Category-- changing to music, it just goes back to --Choose Category--
Thanks for spotting the rn slashes, I originally had those but then copied some of the code from another part of the tutorial which didn't have them, I put the slashes back into the code and it still doesn't work. Thank you for your help criterion9, i appreciate it
I know it's not to do with the javascript, cos from what I see the javascript is just telling the form not to allow a product to be modified or submitted if the category is still 0 (--Choose Category--). I'll include the javascript though just incase I am overlooking something.
Code:
// JavaScript Document
function viewProduct()
{
with (window.document.frmListProduct) {
if (cboCategory.selectedIndex == 0) {
window.location.href = 'index.php';
} else {
window.location.href = 'index.php?catId=' + cboCategory.options[cboCategory.selectedIndex].value;
}
}
}
function checkAddProductForm()
{
with (window.document.frmAddProduct) {
if (cboCategory.selectedIndex == 0) {
alert('Choose the product category');
cboCategory.focus();
return;
} else if (isEmpty(txtName, 'Enter Product name')) {
return;
} else {
submit();
}
}
}
function addProduct(catId)
{
window.location.href = 'index.php?view=add&catId=' + catId;
}
function modifyProduct(productId)
{
window.location.href = 'index.php?view=modify&productId=' + productId;
}
function deleteProduct(productId, catId)
{
if (confirm('Delete this product?')) {
window.location.href = 'processProduct.php?action=deleteProduct&productId=' + productId + '&catId=' + catId;
}
}
function deleteImage(productId)
{
if (confirm('Delete this image')) {
window.location.href = 'processProduct.php?action=deleteImage&productId=' + productId;
}
}
I'm sorry I posted so much code this time, I just thought it might be easier to look at. I've found that the category combo box isn't working on other pages too, however other drop down boxes are, so maybe I could compare the code and see why one is working and one is not. Thank you for your help
So i was comparing the two pieces of code that are actually to do with the forms and they do use the onchange handler. I'll list the two below.
Working form viewed using firebug:
Yeah that would be great. I have a feeling it is a javascript error but that is a lot of code to weed through when Firebug may show me the problem quickly.
Looks like there is a problem loading the categories? I would think the "Music" label should be the option like: <option value=category ID>Music</option>
That would allow you to select it since it would then be an option.
Might it be a problem with the SQL i originally used to create the tables then? Because when I created I didn't have any actual items like the demo version had here: http://www.phpwebcommerce.com/plaincart/index.php
Which is strange because I used the code they put up...might it be worth deleting all the tables in the database and doing a completely new install of everything? I thought it was strange that I wasn't viewing any products when I first set it up. Thanks
It looks like your actual categories are being counted as category groups or something. I'm not familiar enough with the application to be able to tell you whether you should start over or if you are just missing a setting or something.
Yay!! It was to do with the database. I needed an extra category or something under Music. It's because child category records in the category table need to have a parent ID unless they're the parent in which case they're 0. I hadn't created a child category Thank you so much for all your help criterion9! I would've probably been messing around with PHP still if you hadn't pointed me and put me in the right direction thank you so much!! Have a good weekend!
Bookmarks