Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP dependent dropdown from mysql db
Old 12-02-2010, 06:56 PM PHP dependent dropdown from mysql db
dzigner's Avatar
Skilled Talker

Latest Blog Post:
7 Link Challenge
Posts: 62
Trades: 0
Hi
There is a locked forum thread right here with the exact code I am trying to incorporate, and it is not working. The thread here is:
triple-drop-down-list-database
This is the edited code I am using:
Code:
<script language="javascript" type="text/javascript">
  //Global XMLHTTP Request object

var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to 

//a "XmlHttp" variable  

function CreateXmlHttp()
{
 //Creating object of XMLHTTP in IE

 try
 {
  XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
 }
 catch(e)
 {
  try
  {
   XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } 
  catch(oc)
  {
   XmlHttp = null;
  }
 }
 //Creating object of XMLHTTP in Mozilla and Safari 

 if(!XmlHttp && typeof XMLHttpRequest != "undefined") 
 {
  XmlHttp = new XMLHttpRequest();
 }
}

	function getsubcat(catid) {		
		
		var strURL="getsubcat.php?cat_id="+catid;
		var req = getXMLHTTP();
		
		if (req) {
			
			req.onreadystatechange = function() {
				if (req.readyState == 4) {
					// only if "OK"
					if (req.status == 200) {						
						document.getElementById('subcats').innerHTML=req.responseText;						
					} else {
						alert("There was a problem while using XMLHTTP:\n" + req.statusText);
					}
				}				
			}			
			req.open("GET", strURL, true);
			req.send(null);
		}		
	}

</script>
PHP Code:
<?php include("maintemplt.php"); 

if (!
function_exists("GetSQLValueString")) {
function 
GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
{
  if (
PHP_VERSION 6) {
    
$theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  
$theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch (
$theType) {
    case 
"text":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;    
    case 
"long":
    case 
"int":
      
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case 
"double":
      
$theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case 
"date":
      
$theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
      break;
    case 
"defined":
      
$theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
      break;
  }
  return 
$theValue;
}
}

$currentPage $_SERVER["PHP_SELF"];

$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmprodadd")) {
  
$insertSQL sprintf("INSERT INTO tbl_product (cat_id, subcat_id, pd_name, pd_description, pd_price, pd_qty, pd_image, pd_thumbnail) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['prodname'], "text"),
                       
GetSQLValueString($_POST['proddesc'], "text"),
                       
GetSQLValueString($_POST['prodprice'], "double"),
                       
GetSQLValueString($_POST['prodqty'], "int"),
                       
GetSQLValueString($_POST['prodimage'], "text"),
                       
GetSQLValueString($_POST['prodthumb'], "text"));

  
mysql_select_db($database_menwear$menwear);
  
$Result1 mysql_query($insertSQL$menwear) or die(mysql_error());
}

$maxRows_prodmng 10;
$pageNum_prodmng 0;
if (isset(
$_GET['pageNum_prodmng'])) {
  
$pageNum_prodmng $_GET['pageNum_prodmng'];
}
$startRow_prodmng $pageNum_prodmng $maxRows_prodmng;

mysql_select_db($database_menwear$menwear);
// select categories
$query_cats "SELECT cat_id, cat_name FROM tbl_category";
$cats mysql_query($query_cats$menwear) or die(mysql_error());
$row_cats mysql_fetch_assoc($cats);
$totalRows_cats mysql_num_rows($cats);

// select subcategories
$query_subcats "SELECT cat_id, subcat_id, subcat_name FROM tbl_subcat";
$subcats mysql_query($query_subcats$menwear) or die(mysql_error());
$row_subcats mysql_fetch_assoc($subcats);
$totalRows_subcats mysql_num_rows($subcats);

// select products
$query_prodmng "SELECT * FROM tbl_product";
$query_limit_prodmng sprintf("%s LIMIT %d, %d"$query_prodmng$startRow_prodmng$maxRows_prodmng);
$prodmng mysql_query($query_limit_prodmng$menwear) or die(mysql_error());
$row_prodmng mysql_fetch_assoc($prodmng);

if (isset(
$_GET['totalRows_prodmng'])) {
  
$totalRows_prodmng $_GET['totalRows_prodmng'];
} else {
  
$all_prodmng mysql_query($query_prodmng);
  
$totalRows_prodmng mysql_num_rows($all_prodmng);
}
$totalPages_prodmng ceil($totalRows_prodmng/$maxRows_prodmng)-1;

$queryString_prodmng "";
if (!empty(
$_SERVER['QUERY_STRING'])) {
  
$params explode("&"$_SERVER['QUERY_STRING']);
  
$newParams = array();
  foreach (
$params as $param) {
    if (
stristr($param"pageNum_prodmng") == false && 
        
stristr($param"totalRows_prodmng") == false) {
      
array_push($newParams$param);
    }
  }
  if (
count($newParams) != 0) {
    
$queryString_prodmng "&" htmlentities(implode("&"$newParams));
  }
}
$queryString_prodmng sprintf("&totalRows_prodmng=%d%s"$totalRows_prodmng$queryString_prodmng);
?>
HTML Code:
<form action="<?php echo $editFormAction; ?>" method="POST" name="frmprodadd" id="frmprodadd">
    <table id="tblprodadd" class="mng">
      <tr>
        <td><label for="prodname">Product Name</label></td>
        <td><input name="prodname" type="text" id="prodname" size="50" /></td>
        </tr>
      <tr>
        <td><label for="proddesc">Description</label></td>
        <td><textarea name="proddesc" id="proddesc" cols="45" rows="5"></textarea></td>
        </tr>
      <tr>
        <td><label for="catname">Category</label></td>
        <td><label for="cats"></label>
          <select name="cats" id="cats" onChange="getsubcat(this.value)">
          <option value="">Select Category</option>
            <?php do {  ?>
            <option value="<?php echo $row_cats['cat_id']?>"><?php echo $row_cats['cat_name']?></option>
            <?php
} while ($row_cats = mysql_fetch_assoc($cats));
  $rows = mysql_num_rows($cats);
  if($rows > 0) {
      mysql_data_seek($cats, 0);
	  $row_cats = mysql_fetch_assoc($cats);
  }
?>
          </select></td>
      </tr>
      <tr>
        <td><label for="subcat">Sub Category</label></td>
        <td><div id="subcats"><select name="subcat" id="subcat">
        <option value="">Select SubCategory</option>
        </select></div>
        </td>
      </tr>
      <tr>
        <td><label for="prodprice">Price</label></td>
        <td><input name="prodprice" type="text" id="prodprice" value="0.00" size="10" /></td>
      </tr>
      <tr>
        <td><label for="prodqty">Quantity</label></td>
        <td><input name="prodqty" type="text" id="prodqty" size="10" /></td>
      </tr>
      <tr>
        <td><label for="prodimage">Image Path/URL</label></td>
        <td><input name="prodimage" type="text" id="prodimage" value=" " size="50" /></td>
      </tr>
      <tr>
        <td><label for="prodimage">Image Thumbnail</label></td>
        <td><input name="prodthumb" type="text" id="prodthumb" value=" " size="50" /></td>
      </tr>
      <tr>
        <td colspan="2">
          <input type="submit" name="submit" id="submit" value="Add Product" /></td>
        </tr>
    </table>
    <input type="hidden" name="MM_insert" value="frmprodadd" />
    </form>
And this code in getsubcat.php file:
PHP Code:
<? $cat_id=intval($_GET['cat_id']);

mysql_select_db($database_menwear$menwear);

$query_subcats="SELECT cat_id, subcat_id, subcat_name FROM tbl_subcat WHERE cat_id='$cat_id'";
$subcats mysql_query($query_subcats$menwear) or die(mysql_error());
$row_subcats mysql_fetch_assoc($subcats);
$totalRows_subcats mysql_num_rows($subcats);
?>
<select name="subcat" id="subcat">
        <option value="">Select SubCategory</option>
        <?php do { ?>
        <option value=<?php echo $row_subcats['subcat_name']?>><?php echo $row_subcats['subcat_name']?></option>
            <?php } while($row_subcats=mysql_fetch_array($subcats));
              
$rows mysql_num_rows($subcats);
              if(
$rows 0) {
                  
mysql_data_seek($subcats0);
                  
$row_cats mysql_fetch_assoc($subcats);
                  }     
?>
</select>
This code is not working. The subcategories dropdown remains empty, there are no options under there. I cannot give the url, as this is on my testing server.

Any help will be appreciated.
Thanks
__________________
dzigner

Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
,
Please login or register to view this content. Registration is FREE
dzigner is offline
Reply With Quote
View Public Profile Visit dzigner's homepage!
 
 
Register now for full access!
Reply     « Reply to PHP dependent dropdown from mysql db
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08979 seconds with 12 queries