Forum Moderators: coopster
Thank you.
<html>
<body>
<?php
mysql_connect("localhost", "root") or die( "Unable to connect\n". mysql_error() );
mysql_select_db("test") or die("Unable to select db ".mysql_error()."\n");
$id = $_GET['id'];
echo'<form name="testform">';
$q = mysql_query("SELECT * FROM grupper");
echo"<select name=\"gruppe\" onChange=\"Load_id()\">";
while($row = mysql_fetch_array($q)) {
$selected = ($row["gruppe_id"] == $id)? "SELECTED":"";
echo"<option value=\"".$row['gruppe_id']."\"". $selected." >".$row['gruppe_navn']."</option>";
}
echo"</select>";
$q2 = mysql_query("SELECT * FROM varer WHERE vare_gruppe_id = $id");
echo"<select name=\"vare\">";
while($row = mysql_fetch_array($q2)) {
echo"<option value=\"".$row['vare_id']."\">".$row['vare_navn']."</option>";
}
echo"</select></form>";
?>
<script type="text/javascript">
function Load_id() {
var id = document.testform.gruppe.options[document.testform.gruppe.selectedIndex].value
var id_txt = "?id="
location = id_txt + id
}
</script>
</BODY>
</HTML>
DB_DUMPS////////////////////////////
CREATE TABLE `grupper` (
`gruppe_id` int(11) NOT NULL auto_increment,
`gruppe_navn` varchar(60) NOT NULL default '',
`gruppe_code` varchar(60) NOT NULL default '',
PRIMARY KEY (`gruppe_id`)
) TYPE=MyISAM AUTO_INCREMENT=3 ;
INSERT INTO `grupper` VALUES (1, 'testgruppe1','P120');
INSERT INTO `grupper` VALUES (2, 'testgruppe2','P110');
CREATE TABLE `varer` (
`vare_id` int(11) NOT NULL auto_increment,
`vare_gruppe_id` int(11) NOT NULL default '0',
`vare_navn` varchar(60) NOT NULL default '',
`vare_code` varchar(60) NOT NULL default '',
PRIMARY KEY (`vare_id`)
) TYPE=MyISAM AUTO_INCREMENT=5 ;
INSERT INTO `varer` VALUES (1, 1, 'testvare1','A123');
INSERT INTO `varer` VALUES (2, 1, 'testvare2','A345');
INSERT INTO `varer` VALUES (3, 2, 'testvare3','A456');
INSERT INTO `varer` VALUES (4, 2, 'testvare4','A789');
[edited by: jatar_k at 4:44 pm (utc) on Sep. 16, 2004]
[edit reason] reformatted text [/edit]
The code I have submitted shows only 2 drop down boxes. If the user selects from 1st drop down box, an quivalent lists of data is displayed on 2nd drop down.
My problem is the code for my textbox after the 1st drop down box and also the code for another textbox after the 2nd drop down box. Now, if ever the user will selects from the 1st drop down box, the equivalent code for the selected data will be displayed on the textbox.
$q = mysql_query("SELECT * FROM grupper");echo"<select name=\"gruppe\" onChange=\"Load_id()\">";
while($row = mysql_fetch_array($q)) {
$selected = ($row["gruppe_id"] == $id)? "SELECTED":"";
echo"<option value=\"".$row['gruppe_id']."\"". $selected." >".$row['gruppe_navn']."</option>";
}
echo"</select>";//This is where the code of the textbox should fall. I really don't know on how to display the equivalent code selected from the 1st drop down.//
$q2 = mysql_query("SELECT * FROM varer WHERE vare_gruppe_id = $id");
echo"<select name=\"vare\">";
while($row = mysql_fetch_array($q2)) {
echo"<option value=\"".$row['vare_id']."\">".$row['vare_navn']."</option>";
}
echo"</select>";//This is where the code of the another textbox should fall. I really don't know on how to display the equivalent code selected from the 2nd drop down.//
[edited by: coopster at 2:57 pm (utc) on Sep. 18, 2004]
[edit reason] snipped irrelevant code [/edit]
echo"<textarea name=\"gruppetext\" rows=\"5\" cols=\"50\">".$row['gruppe_code'].'</textarea>';
echo"<textarea name=\"gruppetext\" rows=\"5\" cols=\"50\">".$row['gruppe_code'].'</textarea>';
I hardcoded the rows and cols attributes. See <textarea> [w3.org] for more information.
I really need help with this. This code is composed of 4 drop down boxes and 3 text boxes.
It is arranged as:
1. division: which is a drop down box
2. project1: which is a drop down box
3. pcode: which is a text box
4. activity1: which is a drop down box
5. acode: which is a text box
6. medium1: which is a drop down box
7. mcode: which is a text box.
If the i have to choose 1st from the 1st drop down which is the division before I could choose from project1 then to activity1 then medium1.This is what the drop down box does. The code is fine for the drop down boxes but my very big problem is the 3 text boxes. I don't know what would be the code for the pcode, acode and mcode textbox. The data of the pcode will depend what is choosen from the project1 same also with the acode and activity1 & mcode and medium1. I highlighted all the code for my textbox. If you will notice it is incomplete since I really don't know how to do it. I'm a newbie.
Hope someone could help me with this. I would appreciate it a lot. Maybe I think there should be a javascript function or php to do this but i really don't have any idea how to do this.
here is my database
DivisionTable
div_id ¦ div_name
---1 ¦ GIS
---2 ¦ GAD
---3 ¦ IT
ProjectTable
project_id ¦ project_div_id ¦ project_name ¦ project_code
---------1 ¦ ------1 ------- ¦ -- urban ¦ 2004P066
---------2 ¦ ------1 ------- ¦ --filtering ¦ 2004P069
---------3 ¦ ------2 ------- ¦ -- gad work ¦ 8200P001
---------4 ¦ ------2 ------- ¦ --accounting ¦ 8200P002
---------5 ¦ ------3 ------- ¦ -- programming¦ 8100P005
---------6 ¦ ------3 ------- ¦ -- hardware ¦ 8100P007
ActivityTable
project_id ¦ activity_div_id ¦ activity_name ¦ activity_code
---------1 ¦ ------1 ------- ¦ -- Basemapping ¦ 2004P066
---------2 ¦ ------1 ------- ¦ --Edgematch ¦ 2004P069
---------3 ¦ ------2 ------- ¦ -- Bookkeeping ¦ 8200P001
---------4 ¦ ------2 ------- ¦ --Clerical Works ¦ 8200P002
---------5 ¦ ------3 ------- ¦ -- programming¦ 8100P005
---------6 ¦ ------3 ------¦System Administration ¦8100P007
MediumTable
medium ---------- ¦ medium_code ¦
Working with Paper¦ W(P)
Working with PC---¦ W(PC)
<html>
<body>
<?phpmysql_connect("localhost", "root")
or die( "Unable to connect\n". mysql_error() );mysql_select_db("TEST")
or die("Unable to select db ".mysql_error()."\n");$seldiv = $_GET['seldiv'];
$selproj = $_GET['selproj'];
//$selact = $_GET['selact'];echo'<form name="dropform">';
$q = mysql_query("SELECT * FROM DivisionTable");
echo"<select name=\"division\" onChange=\"Load_id()\">";
echo "<option>".""."</option>";
while($row = mysql_fetch_array($q)) {
$selected = ($row["div_id"] == $seldiv)? "SELECTED":"";
echo"<option value=\"".$row['div_id']."\"". $selected." >".$row['div_name']."</option>";
}
echo"</select>";$q2 = mysql_query("SELECT * FROM ProjectTable WHERE project_div_id = $seldiv");
echo"<select name=\"project1\" onchange=\"Load_code()\">";
while($row = mysql_fetch_array($q2)) {
echo"<option value=\"".$row['project_id']."\">".$row['project_name']."</option>";
}
echo"</select>";
$q2 = mysql_query ("Select * from ProjectTable where ___");
echo "<input class=textbox name=\"pcode\" value=\"".$row['project_code']."\">";$q4 = mysql_query("SELECT * FROM ActivityTable WHERE activity_div_id = $seldiv");
echo"<select name=\"activity1\" >";
while($row = mysql_fetch_array($q4)) {
echo"<option value=\"".$row['activity_id']."\">".$row['activity_name']."</option>";
}
echo"</select>";
$q5 = mysql_query ("Select * from ActivityTable where ____");
echo "<input class=textbox name=\"acode\" value=\"".$row['activity_code']."\">";$q6 = mysql_query("SELECT * FROM MediumTable");
echo"<select name=\"medium1\" >";
echo "<option>".""."</option>";
while($row = mysql_fetch_array($q6)) {
echo"<option>".$row['Medium']."</option>";
}
echo"</select>";
$q7 = mysql_query ("Select * from MediumTable where ____");
echo "<input class=textbox name=\"mcode\" value=\"".$row['medium_code']."\">";echo "</form>";
?><script type="text/javascript">
function Load_id()
{
var seldiv = document.dropform.division.options[document.dropform.division.selectedIndex].value;
var seldiv_txt = "?seldiv=";
location = seldiv_txt + seldiv;
}</script>
</BODY>
</HTML>
Please take a moment to review our PHP Charter [webmasterworld.com] regarding Posting Guidlines for the PHP Server Side Scripting Forum.
"How do I get the <textarea> associated with the selection to display?"
Let's start with a small snippet from the second chunk of code dumped in message number 3. Note the additional code in bold:
echo '<select name="gruppe" onChange="Load_id()">';
$gruppe_code = ''; // initialize
while ($row = mysql_fetch_array($q)) {
$selected = ($row['gruppe_id'] == $id)? ' selected="selected"' : '';
if ($selected) {
$gruppe_code = $row['gruppe_code'];
}
echo '<option value="' .$row['gruppe_id']. "\"$selected>"
. $row['gruppe_navn'] . '</option>';
}
echo '</select>';
echo '<textarea name="gruppetext" rows="5" cols="50">'
. $gruppe_code . '</textarea>';
if ($gruppe_code) {
echo '<textarea name="gruppetext" rows="5" cols="50">'
. $gruppe_code . '</textarea>';
}