Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

population drop down box from a query based on the selected value

Status
Not open for further replies.

bluerain25

Technical User
Jun 5, 2008
16
PH
hi!

new to php/mysql here. heres my code i dont know how to do this but i need help

<table align="center" width="80%">
<tr>
<td bgcolor="#3366FF" colspan=2 align="center"><b>Assigment</b></td>
</tr>
<tr>
<td><div align="right">Area:</div></td>
<td><? $query = "SELECT `areatbl`.`AreaID`, `areatbl`.`AreaName` FROM `areatbl`";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select name='area' id = 'area' onChange="">
<option value=""></option>
<?
//Now fill the table with data
while ($row = mysql_fetch_array($result))
{
echo $areaid= $row['AreaID'];
$areaDesc = $row['AreaName'];?>
<option value='<? print $areaid ?>'><? print $areaDesc ?></option>
<? }
?>
</select>
<? }
elseif($debug)
{
die("retrieveQueryType(): No rows returned by query");
}
}
else
{
$success = FALSE;
if($debug)
{
die("retrieveQueryType(): query failed - ".mysql_error());
}
}
?></td>
</tr>
<tr>
<td><div align="right">Branch:</div></td>
<td>


<? $query = "SELECT `branchdepttbl`.`BranchDeptID`, `branchdepttbl`.`BranchDeptName` FROM
`branchdepttbl` WHERE `branchdepttbl`.`AreaID` =" .$_POST['area'];


if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select name='branch' id = 'branch'>
<option value=""></option>
<?
//Now fill the table with data
while ($row = mysql_fetch_array($result))
{
echo $branchid= $row['BranchDeptID'];
$branchDesc = $row['BranchDeptName'];?>
<option value='<? print $branchid ?>'><? print $branchDesc ?></option>
<? }
?>
</select>
<? }
elseif($debug)
{
die("retrieveQueryType(): No rows returned by query");
}
}
else
{
$success = FALSE;
if($debug)
{
die("retrieveQueryType(): query failed - ".mysql_error());
}
}
?></td> </tr>
<tr>
<td><div align="right">Position:</div></td>
<td><? $query = "SELECT `positiontbl`.`PositionID`, `positiontbl`.`PositionDesc`FROM `positiontbl`";
if($result = mysql_query($query))
{
if($success = mysql_num_rows($result) > 0)
{
?>
<select name='position' id = 'position'>
<option value=""></option>
<?
//Now fill the table with data
while ($row = mysql_fetch_array($result))
{
echo $positionid= $row['PositionID'];
$positionDesc = $row['PositionDesc'];?>
<option value='<? print $positionid ?>'><? print $positionDesc ?></option>
<? }
?>
</select>
<? }
elseif($debug)
{
die("retrieveQueryType(): No rows returned by query");
}
}
else
{
$success = FALSE;
if($debug)
{
die("retrieveQueryType(): query failed - ".mysql_error());
}
}
?></td>
</tr>
</table>

at the branch query, it is supposed to display the list of branches depending on a selected area (or the value selected in the first drop down box). but my code does not work. the branch select box is gone when i preview the page.

help please.....

thanks in advance
 
If $_POST['area'] is alphanumeric, you should enclose it within quotes to avoid the 'unknown column' error.

Why
Code:
echo $branchid = $row['BranchDeptID'];

I cannot understand why echo this string at this point. Troubleshooting perhaps?

Why use to many IF statements? Why not simplify things and have your die( ... ) within the mysql_query(...) statement?

If you want to trigger die(...) only if $debug, then do this condition at the mysql_query(...) level not while fetching for the results. IMHO ...

The fact that the drop down box is not showing it is simply because your condition is NOT TRUE. Unless you solve or proof this, you will not be able to get to the bottom line.

I would start by keeping mysql_query() and die() together.





--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
You have quite a few SELECT boxes, but nowhere do I see an actual Form surrounding them.

no you make use of the $_POST['area'] variable but nowhere do you check for it. If its not been submitted then you'll likely get a Notice about it. And of course no results from your query. Which means your select box will not be displayed.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top