www.webdeveloper.com
Results 1 to 14 of 14

Thread: plaincart combo box not selecting

  1. #1
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25

    plaincart combo box not selecting

    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());
        
        
    $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>rn";
            }
            
            
    $list .= "</optgroup>";
        }
        
        return 
    $list;

    Any help would be greatly appreciated. Thanks.

  2. #2
    Join Date
    Jan 2009
    Posts
    3,346
    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?

  3. #3
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    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

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    The code you posted wouldn't do that. What does the "<form>" line and "<select>" line say? Are you using an "onchange" handler for the drop down?

  5. #5
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    So the PHP's alright, it's something to do with the HTML of the actual box?
    Below is the code that is to do with the actual box.
    Code:
    <select name="cboCategory" id="cboCategory" class="box">
         <option value="" selected>-- Choose Category --</option>
    <?php
    	echo $list;
    ?>	 
        </select>
    Here's the code for the entire page (I thought I'd post that just incase I was leaving out some useful code):
    Code:
    <?php
    if (!defined('WEB_ROOT')) {
    	exit;
    }
    
    // make sure a product id exists
    if (isset($_GET['productId']) && $_GET['productId'] > 0) {
    	$productId = $_GET['productId'];
    } else {
    	// redirect to index.php if product id is not present
    	header('Location: index.php');
    }
    
    // get product info
    $sql = "SELECT pd.cat_id, pd_name, pd_description, pd_price, pd_qty, pd_image, pd_thumbnail
            FROM tbl_product pd, tbl_category cat
    		WHERE pd.pd_id = $productId AND pd.cat_id = cat.cat_id";
    $result = mysql_query($sql) or die('Cannot get product. ' . mysql_error());
    $row    = mysql_fetch_assoc($result);
    extract($row);
    
    // get category list
    $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) {
    		$categories[$id] = array('name' => $name, 'children' => array());
    	} else {
    		$categories[$parentId]['children'][] = array('id' => $id, 'name' => $name);	
    	}
    }	
    
    //echo '<pre>'; print_r($categories); echo '</pre>'; exit;
    
    // 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'] == $cat_id) {
    			$list .= " selected";
    		}
    		$list .= ">{$child['name']}</option>";
    	}
    	
    	$list .= "</optgroup>";
    }
    ?> 
    <form action="processProduct.php?action=modifyProduct&productId=<?php echo $productId; ?>" method="post" enctype="multipart/form-data" name="frmAddProduct" id="frmAddProduct">
     <p align="center" class="formTitle">Modify Product</p>
     
     <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1" class="entryTable">
      <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 $list;
    ?>	 
        </select></td>
      </tr>
      <tr> 
       <td width="150" class="label">Product Name</td>
       <td class="content"> <input name="txtName" type="text" class="box" id="txtName" value="<?php echo $pd_name; ?>" size="50" maxlength="100"></td>
      </tr>
      <tr> 
       <td width="150" class="label">Description</td>
       <td class="content"> <textarea name="mtxDescription" cols="70" rows="10" class="box" id="mtxDescription"><?php echo $pd_description; ?></textarea></td>
      </tr>
      <tr> 
       <td width="150" class="label">Price</td>
       <td class="content"><input name="txtPrice" type="text" class="box" id="txtPrice" value="<?php echo $pd_price; ?>" size="10" maxlength="7"> </td>
      </tr>
      <tr> 
       <td width="150" class="label">Qty In Stock</td>
       <td class="content"><input name="txtQty" type="text" class="box" id="txtQty" value="<?php echo $pd_qty;  ?>" size="10" maxlength="10"> </td>
      </tr>
      <tr> 
       <td width="150" class="label">Image</td>
       <td class="content"> <input name="fleImage" type="file" id="fleImage" class="box">
    <?php
    	if ($pd_thumbnail != '') {
    ?>
        <br>
        <img src="<?php echo WEB_ROOT . PRODUCT_IMAGE_DIR . $pd_thumbnail; ?>"> &nbsp;&nbsp;<a href="javascript:deleteImage(<?php echo $productId; ?>);">Delete 
        Image</a> 
        <?php
    	}
    ?>    
        </td>
      </tr>
     </table>
     <p align="center"> 
      <input name="btnModifyProduct" type="button" id="btnModifyProduct" value="Modify Product" onClick="checkAddProductForm();" class="box">
      &nbsp;&nbsp;<input name="btnCancel" type="button" id="btnCancel" value="Cancel" onClick="window.location.href='index.php';" class="box">  
     </p>
    </form>
    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

  6. #6
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    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:
    HTML Code:
    <td width="75">
    <select id="cboOrderStatus" class="box" onchange="viewOrder();" name="cboOrderStatus">
    <option selected="" value="">All</option>
    <option value="New">New</option>
    <option selected="" value="Paid">Paid</option>
    <option value="Shipped">Shipped</option>
    <option value="Completed">Completed</option>
    <option value="Cancelled">Cancelled</option>
    </select>
    </td>
    The PHP behind it is:
    Code:
    <form action="processOrder.php" method="post"  name="frmOrderList" id="frmOrderList">
     <table width="100&#37;" border="0" cellspacing="0" cellpadding="2" class="text">
     <tr align="center"> 
      <td align="right">View</td>
      <td width="75"><select name="cboOrderStatus" class="box" id="cboOrderStatus" onChange="viewOrder();">
        <option value="" selected>All</option>
        <?php echo $orderOption; ?>
      </select></td>
    And below is the form I'm having troubles with.
    Not working category form viewed using firebug:
    HTML Code:
    <select id="cboCategory" class="box" onchange="viewProduct();" name="cboCategory">
    <option selected="">All Category</option>
    <optgroup label="Music"/>
    </select>
    And here is the PHP behind it:
    Code:
    <form action="processProduct.php?action=addProduct" method="post"  name="frmListProduct" id="frmListProduct">
     <table width="100%" border="0" cellspacing="0" cellpadding="2" class="text">
      <tr>
       <td align="right">View products in : 
        <select name="cboCategory" class="box" id="cboCategory" onChange="viewProduct();">
         <option selected>All Category</option>
    	<?php echo $categoryList; ?>
       </select>
    Would it be any help if I created a temporary admin for you, so you could login and see the actual form? Thanks.
    Last edited by tv_boy123; 03-29-2009 at 07:51 AM.

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    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.

  8. #8
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    Thank you so much okies the site with the login is:
    http://www.enochianrecords.com/store/admin/

    It's just a test part of the site for me to learn more about php and stuff so it's not actually selling any real products.

    I've made the username: criterion9
    And the password is also: criterion9

    Thank you for all your help, it is really awesome

  9. #9
    Join Date
    Jan 2009
    Posts
    3,346
    You don't actually have any options. Your category is being put as the optgroup but there are no options.

    Code:
    <select name="cboCategory" class="box" id="cboCategory" onChange="viewProduct();">
    <option selected>All Category</option>
    	<optgroup label="Music"></optgroup>   </select>
    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.

  10. #10
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    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

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    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.

  12. #12
    Join Date
    Mar 2009
    Location
    Paterson, NJ, USA
    Posts
    25
    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!

  13. #13
    Join Date
    Jul 2009
    Posts
    1
    what exactly did you do? I am having the same problem.

  14. #14
    Join Date
    Jul 2012
    Posts
    2

    Help in the same discussion

    I get the same problem, i don't know if you can help me to resolve it.

    Please it's been a long time a work on it, i use the same file than you on:http://www.phpwebcommerce.com

    Thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles