Forum Moderators: coopster

Message Too Old, No Replies

double drop down and textbox

         

franches

6:45 am on Sep 16, 2004 (gmt 0)

10+ Year Member



I have encountered this code here. This code shows 2 drop down or combo box in page.php, one for Project and the other for Activity. The data of grupper and varer will be retrieved from mysql. The drop down for Activity depends on the value selected on Project drop down. Thus, I also want to add a textbox(ProjectCode) after the 1st drop down (Project) another textbox(ActivityCode) after the second drop down(Activity). The textbox(ProjectCode) depends on the value selected on dropdown(Project) and the textbox(ActivityCode) depends on the value selected on dropdown(Activity). I really need help with the code. I have highlighted in blue the field that holds the ProjectCode and ActivityCode.

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]

jatar_k

4:45 pm on Sep 16, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How is it not working?
Are you getting errors?
Is nothing happening?
What is it exactly that is giving you trouble?

franches

8:15 am on Sep 17, 2004 (gmt 0)

10+ Year Member



the code works fine. but my problem is i would like to add a textbox after the 1st drop down box and another textbox after the 2nd drop down box.

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]

coopster

12:23 pm on Sep 17, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You will plop your <textarea> in the same as you did your <select> elements, just use the correct column name from your table as the value. Replace each of your comments above with each of these statements:

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.

franches

2:54 am on Sep 18, 2004 (gmt 0)

10+ Year Member



I used the code but it just displayed the textarea box. This what i really want to do:when I choose something from the 2nd drop down box the equivalent code for the selected item should be displayed in the textarea or textbox.

franches

2:58 am on Sep 18, 2004 (gmt 0)

10+ Year Member



This is my new code.

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>
<?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");

$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>

coopster

3:18 pm on Sep 18, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



franches, let's slow down and resolve one issue at a time, OK?

Please take a moment to review our PHP Charter [webmasterworld.com] regarding Posting Guidlines for the PHP Server Side Scripting Forum.



You have what seems to be a single issue to resolve first, which is

"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>';

  1. First, we need to initialize a variable that we are going to populate with the code for the <textarea>.
  2. Second, we determine whether or not the current selection in our loop matches the selected item. If so, then move the data from the row into the variable. If we don't get a match, then the variable simply remains blank, just as we initialized it.
  3. Lastly, once we have displayed the <select> options, we display the <textarea> with the value that is stored in the variable.

Now, if there was no code found for the selected item, or none selected, we could do some variations...perhaps we don't even want to show the <textarea>. So, we just check to see if there is any value in the variable, and if so, display it, if not, don't!

if ($gruppe_code) { 
echo '<textarea name="gruppetext" rows="5" cols="50">'
. $gruppe_code . '</textarea>';
}

franches

12:07 am on Sep 20, 2004 (gmt 0)

10+ Year Member



thank you for being so patient with me.

thanks for the help.

coopster

4:52 pm on Sep 20, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome :)