linked combo box's from DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

linked combo box's from DB

Post by C_Calav »

i have two linked combo boxes that get the values from a DB.

how do i submit that option picked in the first combo box back to the page, grab it, query it again, and output the javascript options instead of the predefined options below?

Code: Select all

<form name="myform">
<select name="optone" size="1" onchange="setOptions(document.myform.optone.options[document.myform.optone.selectedIndex].value);">
<option value=" " selected="selected">Select Dept</option>
  <?php
   include("conection.php");

     $query = "select DISTINCT(e_dept) from tbl_emp";
     $result = mysql_query($query);
     $num_results = mysql_num_rows($result);

     for ($i=0; $i <$num_results; $i++)
     {
     $row = mysql_fetch_array($result);

     $e_dept = $row['e_dept'];
  ?>
<option value="<?php echo $i; ?>"> <?php echo $e_dept; ?> </option>
<?php } ?>
</select>


<br /> <br />

<select name="opttwo" MULTIPLE>
<option value=" " selected="selected">Please select one of the options above first</option>
</select>



note: i put $i for a selection option just to get the example to spit something out for the next combo box/

here is the javascript..

Code: Select all

function setOptions(chosen) {
var selbox = document.myform.opttwo;

selbox.options.length = 0;
if (chosen == " ") {
  selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' ');
}

if (chosen == "1") {
  selbox.options[selbox.options.length] = new Option('first choice - option one','oneone');
  selbox.options[selbox.options.length] = new Option('first choice - option two','onetwo');
}
if (chosen == "2") {
  selbox.options[selbox.options.length] = new Option('second choice - option one','twoone');
  selbox.options[selbox.options.length] = new Option('second choice - option two','twotwo');
}
if (chosen == "3") {
  selbox.options[selbox.options.length] = new Option('third choice - option one','threeone');
  selbox.options[selbox.options.length] = new Option('third choice - option two','threetwo');
}
}
thank you any help would be great..

i have searched and searched.. even got sugessted to use AJAX.

can anyone point me in the right direction this is driving me crazy!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"customize" the query based on if data for the first combo was submitted (verify that it's valid too ;))
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

Hi Feyd,

what do you mean by 'customize'?

so with 'onChange' it acts just like a form submission?

how do i retrieve the first selection from the combo box?

is it best to do it on the same page or a new page?

and one more thing feyd if you know...once ive retreived the first selection option, and i query it again, what is the best way to 'loop' the javascript options? (if that is what you are ment to do)

thanks very much for any input, need some help with this one!


ps sorry for all the questions :oops:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

basically, you look at $_REQUEST['optone'], if set, someone is trying to submit something... so after validating, add the value sent to your query..
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thanks feyd,

so with $_REQUEST['optone'] i do a query to get the options for the second box.

do i output the options in javascript?

something like this maybe?

or is there a better way of doing this?

Code: Select all

function setOptions(chosen) { 
var selbox = document.myform.opttwo; 

<?php
    for ($i=0; $i <$num_results; $i++) 
     { 
     $row = mysql_fetch_array($result); 

     $e_dept = $row['e_dept']; 
  ?> 
selbox.options[selbox.options.length] = new Option('first choice - option one','oneone'); 
<?php } ?> 

}

thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if php is handling it, you can have php write the options out... otherwise you'll need XMLHTTP or something more along the lines of CoderGoblin's tutorial post about combo boxes and database interlinking.... available here
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

Feyd,

good plan i will fill the combo box up with php.

if i take out all this stuff from the 'head'

Code: Select all

function setOptions(chosen) { 
var selbox = document.myform.opttwo; 

selbox.options.length = 0; 
if (chosen == " ") { 
  selbox.options[selbox.options.length] = new Option('Please select one of the options above first',' '); 
} 

if (chosen == "1") { 
  selbox.options[selbox.options.length] = new Option('first choice - option one','oneone');  
....
....
....
but i wanna keep the onChange event in my first combo box,

i wanna send the option chosen back to the form (not to the javascript function in the 'head' like it was doing)

onchange="setOptions(document.myform.optone.options[document.myform.optone.selectedIndex].value);">

how do i do that?

thanks!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

well.. if not using XMLHTTP, then you'd submit the form with the onchange ...

Code: Select all

onchange="this.form.submit();"
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

thank you very much thats what i was after :)
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

ok i have gotten this far so far..

everything works how it is ment to ...

although ...

when you click the add or remove button (after you have selected a dept) the list boxes clear. how can i keep the same options up after i have clicked add or remove?

i am having a bit of trouble so you might find some code around that is not needed. any ideas? is this possible?

note: once you click the 'Add' button it redirects to users_add.php

and when you click the 'Remove' button it redirects to users_delete.php

does this have anything to do with it?

thanks

Code: Select all

<?php
session_start(); 
if (!isset($_SESSION["loggedin"]))
{ 
exit("Hacking Attempt!");
} 
else
{
echo "welcome to the admin section " .$_SESSION["name"]; 

	if ($_SERVER['REQUEST_METHOD'] == "POST")
        {     
		$s_id = $_POST["s_id"];
	}
	else
	{
		$s_id = $_GET["s_id"];
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
<head>
<title>..</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css" media="all">
		@import "/css/main.css";
	</style>
</head>

<body>

<div id="left">
 <?php include("menu.php");?>
</div>

<div id="content">

<form name="select_dept" action="/admin/users.php" method="post">
<select name="select_dept" onchange="this.form.submit();">
<option value="0" selected>Select Dept</option>
  <?php
   include("conection.php");

     $query = "select DISTINCT(e_dept) from tbl_emp";
     $result = mysql_query($query);
     $num_results = mysql_num_rows($result);

     for ($i=0; $i <$num_results; $i++)
     {
     $row = mysql_fetch_array($result);
     $e_dept = $row['e_dept'];
?>
<option value="<?php echo $e_dept; ?>"> <?php echo $e_dept; ?> </option>

<?php 
}
?>
</select>

<input type="hidden" name="s_id" value="<?php echo "$s_id"; ?>">
</form>




<!-- to check if i still have the values -->
<?php echo "s_id: $s_id"; ?>
<?php echo "e_dept: $e_dept"; ?>





<!-- select users and click add -->
<form name="add_user" action="/admin/users_add.php" method="post">
<select name="select_users" MULTIPLE>
<option value="0"> --Select Users-- </option>
<?php
   include("conection.php");

     if ($_SERVER['REQUEST_METHOD'] == "POST")
     {     
         $e_dept = $_POST["select_dept"];
         //$e_dept = $_POST["e_dept"];
     }
     
     if ($_SERVER['REQUEST_METHOD'] == "GET") 
     {     
    	 //$e_dept = $_GET["e_dept"];
         $e_dept = $_GET["select_dept"];
     }
     
     $query = "select * from tbl_emp WHERE e_dept='$e_dept'"; 
     ##$query = "select * from tbl_emp"; 


     $result = mysql_query($query); 
     $num_results = mysql_num_rows($result) or die (mysql_error()); 
    
     for ($i=0; $i <$num_results; $i++) 
     { 
     $row = mysql_fetch_array($result);
      
     $e_id = $row['e_id'];
     $e_first = $row['e_first']; 
?>
<option value="<?php echo $e_id; ?>"> <?php echo $e_first; ?> </option>
<?php
} 
?>


</select>

<input type="submit" name="add_user" value="Add" class="inputbutton" onfocus="this.blur()" />
<input type="hidden" name="s_id" value="<?php echo "$s_id"; ?>">

</form>



<!--check if still have values -->
<?php echo "s_id: $s_id"; ?>
<?php echo "e_dept: $e_dept"; ?>



<!-- click user and click delete to remove them from list -->
<form name="delete_user" action="/admin/users_delete.php" method="post">
<select name="selected_users" MULTIPLE>
<option value="0"> --Selected Users-- </option>
<?php
   include("conection.php");    

     if ($_SERVER['REQUEST_METHOD'] == "POST") 
     {     
         $e_dept = $_POST["select_dept"]; 
     }
     
     if ($_SERVER['REQUEST_METHOD'] == "GET") 
     {     
    	 $e_dept = $_GET["e_dept"]; 
     }

     $query = "select * from tbl_user WHERE s_id='$s_id'"; 
     $result = mysql_query($query); 
     $num_results = mysql_num_rows($result) or die (mysql_error()); 
    
     for ($i=0; $i <$num_results; $i++) 
     { 
     $row = mysql_fetch_array($result); 
      
     $u_id = $row['u_id'];      
     $e_id = $row['e_id']; 
     $u_first = $row['u_first']; 
?>
<option value="<?php echo $u_id; ?>"> <?php echo $u_first; ?> </option>
<?php
}
?>
</select>

<input type="submit" name="add_user" value="Remove" class="inputbutton" onfocus="this.blur()" />
<input type="hidden" name="s_id" value="<?php echo "$s_id"; ?>">
<input type="hidden" name="e_dept" value="<?php echo "$e_dept"; ?>">
</form>


//check
<?php echo "s_id: $s_id"; ?>
<?php echo "e_dept: $e_dept"; ?>


<form action="/admin/admin.php">
<input type="submit" name="home" value="Finish>>" class="inputbutton" onfocus="this.blur()" />
</form>

</div>

</body>
</html>

<?php
}
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

simple enough.. you match the value(s) you get from the submission to the values found in the results, emitting "selected" on each option that matched.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

when building your select_boxes and looping through the results.. of course you'll have to change the variable names

Code: Select all

echo (isset($_POST['select_box']) && $_POST['select_box'] == $row['select_box'] ? ' selected="selected' : '');
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

finally got some time to look at this.

refering to what Jcart posted

where do i put this?

Code: Select all

echo (isset($_POST['select_box']) && $_POST['select_box'] == $row['select_box'] ? ' selected="selected' : '');

i tried putting it in the first combo box like this .. but cannot get it to work. i dont think the 'selected' is showing up in the html code when you look at it on the webpage.

any help would be much appriated

(ps sorry about the bad use of tags, not sure how to put tags around when its html/php/html/php)

Code: Select all

<form name="select_dept" action="/admin/users.php" method="post">
<select name="select_dept" onchange="this.form.submit();">
<option value="0" selected>Select Dept</option>
<?php
   include("conection.php");

     $query = "select DISTINCT(e_dept) from tbl_emp";
     $result = mysql_query($query);
     $num_results = mysql_num_rows($result);

     for ($i=0; $i <$num_results; $i++)
     {
     $row = mysql_fetch_array($result);
     $e_dept = $row['e_dept'];
     
   echo (isset($_POST['select_dept']) && $_POST['select_dept'] == $row['select_dept'] ? ' selected="selected' : '');
?>
<option value="<?php echo $e_dept; ?>"> <?php echo $e_dept; ?> </option>

<?
}
?>

</select>

<input type="hidden" name="s_id" value="<?php echo "$s_id"; ?>">
</form>
Last edited by C_Calav on Tue Sep 27, 2005 7:31 pm, edited 2 times in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's best to place them around all of the code.. the parser will handle it (fairly) accurately..
User avatar
C_Calav
Forum Contributor
Posts: 395
Joined: Wed Jun 02, 2004 10:55 pm
Location: New Zealand

Post by C_Calav »

is that better feyd?

its just the top part that is not parsing correctly?
Post Reply