Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-03-2012, 10:56 PM   PM User | #1
airy
New Coder

 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
airy is an unknown quantity at this point
Unable to select option in dropdownlist.

Why couldnt i select the options in drop down list?
anyone knows how?



add.php
Code:
<?php
if (!defined('WEB_ROOT')) {
	exit;
}

$catId = (isset($_GET['catId']) && $_GET['catId'] > 0) ? $_GET['catId'] : 0;

$categoryList = buildCategoryOptions($catId);
?> 
<p>&nbsp;</p>
<form action="processProduct.php?action=addProduct" method="post" enctype="multipart/form-data" name="frmAddProduct" id="frmAddProduct">
  <table width="100%" 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>
  <tr> 
   <td width="150" class="label">Product Name</td>
   <td class="content"> <input name="txtName" type="text" class="box" id="txtName" 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"></textarea></td>
  </tr>
  <tr> 
   <td width="150" class="label">Price</td>
   <td class="content"><input name="txtPrice" type="text" id="txtPrice" size="10" maxlength="7" class="box" onKeyUp="checkNumber(this);"> </td>
  </tr>
  <tr> 
   <td width="150" class="label">Qty In Stock</td>
   <td class="content"><input name="txtQty" type="text" id="txtQty" size="10" maxlength="10" class="box" onKeyUp="checkNumber(this);"> </td>
  </tr>
  <tr> 
   <td width="150" class="label">Image</td>
   <td class="content"> <input name="fleImage" type="file" id="fleImage" class="box"> 
    </td>
  </tr>
 </table>
 <p align="center"> 
  <input name="btnAddProduct" type="button" id="btnAddProduct" value="Add 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>



functions.php
Code:
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;
}
airy is offline   Reply With Quote
Old 08-03-2012, 11:01 PM   PM User | #2
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
A good way to be-bug PHP is to look at the browser source code, in Firefox its View Page Source. Then you can pinpoint the error in the HTML code and fix the PHP code.
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 08-03-2012, 11:07 PM   PM User | #3
airy
New Coder

 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
airy is an unknown quantity at this point
Quote:
Originally Posted by Len Whistler View Post
A good way to be-bug PHP is to look at the browser source code, in Firefox its View Page Source. Then you can pinpoint the error in the HTML code and fix the PHP code.
Firefox? I will try it (: thankyou!! (:
airy is offline   Reply With Quote
Old 08-03-2012, 11:15 PM   PM User | #4
airy
New Coder

 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
airy is an unknown quantity at this point
hmmm, i'm not sure how to use it. can anyone help ?
airy is offline   Reply With Quote
Old 08-03-2012, 11:17 PM   PM User | #5
DrDOS
Senior Coder

 
Join Date: Sep 2010
Posts: 1,234
Thanks: 11
Thanked 157 Times in 157 Posts
DrDOS is infamous around these parts
Your select box doesn't even have a tag, it also needs a name and a value. Without a value it will just send the index and you will have to fetch the value out of an array.
DrDOS is offline   Reply With Quote
Old 08-03-2012, 11:21 PM   PM User | #6
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by airy View Post
hmmm, i'm not sure how to use it. can anyone help ?
Right click the webpage and the dialog box should come up, then go to View Page Source.
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Old 08-03-2012, 11:34 PM   PM User | #7
airy
New Coder

 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
airy is an unknown quantity at this point
hmmm... im still unsure of what to do. do you mind tell me which part to change?
and i think there's value retrieving from database? Sorry, im new to php, really dont know what to do about it
airy is offline   Reply With Quote
Old 08-03-2012, 11:42 PM   PM User | #8
Len Whistler
Senior Coder

 
Len Whistler's Avatar
 
Join Date: Jul 2002
Location: Vancouver, BC Canada
Posts: 1,323
Thanks: 26
Thanked 100 Times in 100 Posts
Len Whistler is on a distinguished road
Quote:
Originally Posted by airy View Post
hmmm... im still unsure of what to do. do you mind tell me which part to change?
and i think there's value retrieving from database? Sorry, im new to php, really dont know what to do about it
Your problem appears to be an HTML issue. By viewing the browser source code of the webpage you can see what is missing and fix the PHP to output the proper HTML for the drop down menu.

Can you post the HTML that is outputted by the PHP?
__________________
Leonard Whistler
Len Whistler is offline   Reply With Quote
Users who have thanked Len Whistler for this post:
airy (08-03-2012)
Old 08-04-2012, 09:42 AM   PM User | #9
airy
New Coder

 
Join Date: Jun 2012
Posts: 12
Thanks: 2
Thanked 0 Times in 0 Posts
airy is an unknown quantity at this point
Code:
<td class="label" width="150">Category</td>
<td class="content">
<select id="cboCategory" class="box" name="cboCategory">
<option selected="" value="">-- Choose Category --</option>
<optgroup label="Tops"></optgroup>
<optgroup label="Dress"></optgroup>
<optgroup label="Sweater"></optgroup>
</select>
</td>
airy is offline   Reply With Quote
Old 08-04-2012, 08:46 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
There are no values in the drop-down for you to select. Either the SQL statement doesn't retrieve any data, or the code that follows it doesn't successfully construct the options for the drop-down.

Test your SQL statement in phpMyAdmin:

Code:
SELECT cat_id, cat_parent_id, cat_name
			FROM tbl_category
			ORDER BY cat_id
I didn't notice a submit button anywhere..?
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:16 AM.


Advertisement
Log in to turn off these ads.