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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loading combo boxes

Status
Not open for further replies.

roontoon

MIS
Sep 25, 2010
15
US
Hi how can I dynamically load a combo box from a mysql db? I need to load one combo box with room numbers and then when a room number is selected it will display asset numbers of items in that room in a second combo box. I have seen various AJAX code snippets but I have not found any examples that work. I am a newbie and still learning PHP so if this question is obvious consider the source. Thanks for any help.

d
 
There's several things you need to know and understand:

Javascript knowledge is essential, The Ajax connection is a simple matter, but the Javascript side that will use the retrieved results to populate the second dropdown is a little more complex.
I don't know how well versed you are in Javascript coding, so it may not be an easy task to undertake for you.

With that said to dynamically generate and manipulate combo boxes takes quite a bit of code. So its not like slapping 2 lines of PHP together and expecting results.

The first thing is to create your first combo box from the database.

Assuming you know how to access and retrieve the data from the DB, the construction of the drop down comes down to outputting the correct HTML around your retrieved values.

Code:
$dropdown="<select name="rooms">
while($row=mysql_fetch_array($result)){
$dropdown.= "<option value='$row[fieldname]'>$row['otherfieldname']</option>";
}

$dropdown.= "</select>"


This is the basic code to build a drop down.


Your second drop down will of course depend on what is selected, so the next thing you need is a javascript function to harness the value from the dropdown so you can send it over to your PHP script to generate your results.
Code:
<script>
function populate_dropdwn2(dd1value){
...
}
</script>

You'll want to call this from the onchange event of the dropdown so the value gets captured as soon as its selected from it.

Code:
<select onchange="populate_dropdwn2([red]this.value[/red]);">
...

Once you have that in place you will then have to create a PHP script that can take the selected value and perform a query to retrieve your desired results to populate your second drop down.

Code:
$value=$_GET['ddvalue'];
$qry="SELECT *FROM mytable WHERE xfield='" . $value . "'";
$res=mysql_query($qry);
while($row=mysql_fetch_array($res)){
...
}

With that done you need to decide how you are going to send the result back to the dropdown page to populate the second one, either a string or a json array or some other format you can use in Javascript.

Build your output as required, and have it output to "screen", using an echo statement.

Note that this script should be standalone, and not embedded in the page with the dropdowns, otherwise the Ajax call can't work

For the third and final part you need to setup your Ajax to take the value from the first dropdown, send it to the PHP script, and retrieve the values. With the values retrieved your JS code can do whatever it needs to populate the second dropdown.
Code:
//You use the XMLHttpRequest object to set up the Ajax interface to call your server side script.

//If you need to support older IE browsers you may need use the Active Object rather than the XMLHttpRequest. 

 var xmlhttp;

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }



//Then you setup your function to periodically test to see if the SS script is done and has successfully returned the data

  xmlhttp.onreadystatechange=function()

  {

//readyState 4 and status 200 means eveyrtihng went o.k and you have data returned

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

//You can use the responseText property of your ajax variable to get the output from your server side script.      

var results=xmlhttp.responseText;    

//From there you can do with the result string whatever you want. Stick it in a DIV, or continue to parse it out and work with it if you require.

document.getElementById('result').innerHTML=results;

    }

 }



//The calling of the server side script you are going to use, which includes, like a form, the method to send data.

  xmlhttp.open("POST","script.php",true);

  xmlhttp.setRequestHeader("Content-type","application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]

var value=document.getElementById('tbox').value;

// Include any values you wish to send over to the script

  xmlhttp.send("id=" + ddvalue );

}

If you get this so far, we can move on to using the JS to populate the second dropdown.



----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
If you get this so far, we can move on to using the JS to populate the second dropdown.

Wow! I did not realize how evolved this is. I do hope I get this far and will be working toward that end. Thanks SO MUCH for the lesson and very detailed reply!!. Will be working on this over the weekend and I will post any questions that come up in the process. Thanks again.

d
 
OK I have been working on this and have the PHP an MySQL part of the script working and need some direction with the javascript. I have a temp variable in the second combobox for testing.

Build your output as required, and have it output to "screen", using an echo statement.

Note that this script should be standalone, and not embedded in the page with the dropdowns, otherwise the Ajax call can't work

Are you talking about the AJAX code above or the PHP to load the second combobox when you are talking about standalone. If it is the AJAX then what is the suffix that should be used in the name. Sorry if this is very basic as I am a babe in the woods with javascript and have no clue about AJAX. Again thanks for the school'n!!


d
 
I was referring to the PHP that would load the second combobox.

The Ajax portion needs to call up a script that outputs the data via the echo call to "screen" so to speak. Whatever it outputs is what the ajax will receive and hand over to the JS to handle.

The Ajax should be located in the page page where your combo boxes are as its just a couple of javascript functions



----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
I was referring to the PHP that would load the second combobox.

as in the code bracketed below?

The Ajax portion needs to call up a script that outputs the data via the echo call to "screen" so to speak. Whatever it outputs is what the ajax will receive and hand over to the JS to handle.

Not sure that I did this right but it works in php correctly it just is not activated from the change in the first combobox.

The Ajax should be located in the page page where your combo boxes are as its just a couple of javascript functions
So this is stored in the header with the rest of the java script as in the code below?

[/quote]




Code:
<?PHP
//database settings
require('db.php');

//Connect to MySQL Server
$connection = mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
$db = mysql_select_db($dbname) or die(mysql_error());

$sql = "SELECT DISTINCT `room` FROM `hesk_clients` WHERE `room` > ''";
$result = mysql_query($sql, $connection) or die( mysql_error() );

?>

<html>
<head>
<title>Combobox Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function populate_dropdwn2(dd1value){
...
}

//You use the XMLHttpRequest object to set up the Ajax interface to call your server side script.

//If you need to support older IE browsers you may need use the Active Object rather than the XMLHttpRequest. 

 var xmlhttp;

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }



//Then you setup your function to periodically test to see if the SS script is done and has successfully returned the data

  xmlhttp.onreadystatechange=function()

  {

//readyState 4 and status 200 means eveyrtihng went o.k and you have data returned

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

//You can use the responseText property of your ajax variable to get the output from your server side script.      

var results=xmlhttp.responseText;    

//From there you can do with the result string whatever you want. Stick it in a DIV, or continue to parse it out and work with it if you require.

document.getElementById('result').innerHTML=results;

    }

 }



//The calling of the server side script you are going to use, which includes, like a form, the method to send data.

  xmlhttp.open("POST","script.php",true);

  xmlhttp.setRequestHeader("Content-type","application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]

var value=document.getElementById('tbox').value;

// Include any values you wish to send over to the script

  xmlhttp.send("id=" + ddvalue );

}
</script>
</head>

<body>
<form method="post" action="" name="form">
<table  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="15%">Room</td>
    <td  width="30%">
		<select name="room" class="parent" onchange="populate_dropdwn2(this.value);">
		<option value="" selected="selected">--Select Room--</option>
		<?php
		while ($rows = mysql_fetch_assoc(@$result))
		{?>
			<option value="<?php echo $rows['room'];?>"><?php echo $rows['room'];?></option>
		<?php
		}?>
		</select>	
	</td>
  </tr>
  <tr>
  <td>
  <br>
  </td>
  </tr>
  
  <tr style="">
    <td>Asset Number</td>
    	<td>
		<select name="asset_no" class="child">
		<option value="" selected="selected">--Select Asset--</option>

//************************* needs to be and external php file ???***********************************
		<?php
		
		//$value=$_GET['ddvalue'];
		$value=108; //temp var to test
		
		$sql="SELECT * FROM `hesk_equipment` WHERE `room`='" . $value . "'";
		$result=mysql_query($sql);				
		while ($rows = mysql_fetch_assoc(@$result))
		{?>
			<option value="<?php echo $rows['bpi_tag'];?>"><?php echo $rows['bpi_tag'];?></option>
		<?php
		}?>

//****************************** End ****************************************************
		</select>
    </td>
  </tr>
 </table>
</form>
</body>
</html>
 
as in the code bracketed below?
Correct, otherwise you would need to submit the page to itself to get the PHP to run again and populate the new combobox.


Not sure that I did this right but it works in php correctly it just is not activated from the change in the first combobox.

Exactly, since PHP only runs on the server, it is oblivious to the onChange event which is why the we have the necessity of using Ajax, or actually submitting the page to itself, so PHP can receive the query string value in the $_GET array and perform the required operations.


So this is stored in the header with the rest of the java script as in the code below?
Correct Again. Ajax in its simplest form is a collection of Javascript functions that connect to the server and retrieve output from a specified PHP, ASP or any other server-side script.

Its meant to replace the need for a page to be submitted to the server to have server side code run

----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
OK it is clear to me I don't understand. I have placed the ajax code within the function populate_dropdwn2(dd1value) brackets in the Script section of the header. Also I have created a separate file called combo_look_up.php with the PHP code that was working in terms of populating a combo box before I separated it but was not being triggered by the first combo box. I don't see how to trigger the external PHP code to populate the second combo box. Below is currently what I have in terms of code. As well I have attached a mySQL dump of my data structure and a few records. Also currently this seems to behave like the several examples from the web that I have researched. I am assuming that my server setup does not need to be altered other than being able to run PHP 5 and mySQL. Thanks for the instruction.

Main file. Have a look at the function populate_dropdwn2(dd1value) to see if this is correct. Also I have commented out where the second combo box was, not even sure about this.

Code:
<?PHP
//database settings
require('db.php');

//Connect to MySQL Server
$connection = mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
$db = mysql_select_db($dbname) or die(mysql_error());

$sql = "SELECT DISTINCT `room` FROM `hesk_clients` WHERE `room` > ''";
$result = mysql_query($sql, $connection) or die( mysql_error() );

?>

<html>
<head>
<title>Combobox Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script>
function populate_dropdwn2(dd1value){

//You use the XMLHttpRequest object to set up the Ajax interface to call your server side script.

//If you need to support older IE browsers you may need use the Active Object rather than the XMLHttpRequest. 

 var xmlhttp;

if (window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera, Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");

  }



//Then you setup your function to periodically test to see if the SS script is done and has successfully returned the data

  xmlhttp.onreadystatechange=function()

  {

//readyState 4 and status 200 means eveyrtihng went o.k and you have data returned

    if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

//You can use the responseText property of your ajax variable to get the output from your server side script.      

var results=xmlhttp.responseText;    

//From there you can do with the result string whatever you want. Stick it in a DIV, or continue to parse it out and work with it if you require.

document.getElementById('result').innerHTML=results;

    }

 }



//The calling of the server side script you are going to use, which includes, like a form, the method to send data.

  xmlhttp.open("POST","script.php",true);

  xmlhttp.setRequestHeader("Content-type","application/x-[URL unfurl="true"]www-form-urlencoded");[/URL]

var value=document.getElementById('tbox').value;

// Include any values you wish to send over to the script

  xmlhttp.send("id=" + ddvalue );

}

}
</script>
</head>

<body>
<form method="post" action="" name="form">
<table  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td width="15%">Room</td>
    <td  width="30%">
        <select name="room" class="parent" onchange="populate_dropdwn2(this.value);">
        <option value="" selected="selected">--Select Room--</option>
        <?php
        while ($rows = mysql_fetch_assoc(@$result))
        {?>
            <option value="<?php echo $rows['room'];?>"><?php echo $rows['room'];?></option>
        <?php
        }?>
        </select>    
    </td>
  </tr>
  <tr>
  <td>
  <br>
  </td>
  </tr>
  
  <tr style="">
    <td>Asset Number</td>
        <td>
        <select name="asset_no" class="child">
        <option value="" selected="selected">--Select Asset--</option>

 <?php
/* **    was removed and put into combo_look_up.php

$value=$_GET['ddvalue'];
//$value=108; //temp var to test
        
$sql="SELECT * FROM `hesk_equipment` WHERE `room`='" . $value . "'";
$result=mysql_query($sql);                
while ($rows = mysql_fetch_assoc(@$result))
{?>
<option value="<?php echo $rows['bpi_tag'];?>"><?php echo $rows['bpi_tag'];?></option>
<?php
}
*/

?>
        </select>
    </td>
  </tr>
 </table>
</form>
</body>
</html>


Second file with code removed from the main file.

Code:
<?php
        
$value=$_GET['ddvalue'];
//$value=108; //temp var to test
        
$sql="SELECT * FROM `hesk_equipment` WHERE `room`='" . $value . "'";
$result=mysql_query($sql);                
while ($rows = mysql_fetch_assoc(@$result))
{?>
<option value="<?php echo $rows['bpi_tag'];?>"><?php echo $rows['bpi_tag'];?></option>
<?php
}?>

Data dump

Code:
-- phpMyAdmin SQL Dump
-- version 3.4.3.2
-- [URL unfurl="true"]http://www.phpmyadmin.net[/URL]
--
-- Host: localhost
-- Generation Time: Sep 03, 2011 at 10:07 AM
-- Server version: 5.5.15
-- PHP Version: 5.3.6
--
-- Database: `help_desk`
--

-- --------------------------------------------------------

--
-- Table structure for table `hesk_clients`
--

CREATE TABLE IF NOT EXISTS `hesk_clients` (
  `last_name` text NOT NULL,
  `room` text NOT NULL,
  `first_name` text NOT NULL,
  `email` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `hesk_clients`
--

INSERT INTO `hesk_clients` (`last_name`, `room`, `first_name`, `email`) VALUES
('Brown', '123', 'Shirley', 'shirley.brown@nowhere.com'),
('Duggins', '135', 'Larry', ''),
('Evans', '410', 'Faith', 'faith.evans@nowhere.com'),
('Rahenkamp', '108', 'Daniel', 'daniel.rahenkamp@nowhere.com');

-- --------------------------------------------------------

--
-- Table structure for table `hesk_equipment`
--

CREATE TABLE IF NOT EXISTS `hesk_equipment` (
  `room` text NOT NULL,
  `bpi_tag` text NOT NULL,
  `description` text NOT NULL,
  `serial_no` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Dumping data for table `hesk_equipment`
--

INSERT INTO `hesk_equipment` (`room`, `bpi_tag`, `description`, `serial_no`) VALUES
('108', 'G15314', 'Computer D500', 'V223JYFZA822'),
('108', 'G27652', 'Computer D510', 'V239KN9ZA517'),
('108', 'G27669', 'Computer D510', 'V240KN9ZA679'),
('108', 'G27680', 'Computer D510', 'V240KN9ZA934'),
('108', 'G27681', 'Computer D510', 'V240KN9ZA712'),
('108', 'G27695', 'Computer D510', 'V240KN9ZA749'),
('108', 'G27712', 'Computer D510', 'V239KN9ZA469'),
('108', 'G27715', 'Computer D510', 'V240KN9ZA893'),
('108', 'G27735', 'Computer D510', 'V240KN9ZA916'),
('108', 'G27740', 'Computer D510', 'V239KN9ZA521'),
('108', 'G27742', 'Computer D510', 'V240KN9ZA924'),
('108', 'G27743', 'Computer D510', 'V239KN9ZA508'),
('108', 'G27750', 'Computer D510', 'V243KN8ZA946'),
('108', 'G27752', 'Computer D510', 'V243KN8ZA947'),
('108', 'J72661', 'Computer Dc7100', '2UA5020KH6'),
('108', 'M00895', 'Computer Desktop Fetc', '2UA610K02H'),
('108', 'M06182', '', 'Not in Mainframe'),
('108', 'M06188', '', 'Not in Mainframe'),
('108', 'M35233', 'Computer Dc5700', '2UA7411JKK'),
('108', 'M35258', 'Computer Dc5700', '2UA7411JKG'),
('108', 'M35280', 'Computer Dc5700', '2UA7411JJJ'),
('108', 'M35283', 'Computer Dc5700', '2UA7411JJB'),
('108', 'M35287', 'Computer Dc5700', '2UA7411JK9'),
('123', 'M00871', 'Computer Desktop Fetc', '2UA610K05X'),
('123', 'M00872', 'Computer Desktop Fetc', '2UA610K04D'),
('123', 'M00873', 'Computer Desktop Fetc', '2UA610K05R'),
('123', 'M00874', 'Computer Desktop Fetc', '2UA610K051'),
('123', 'M00875', 'Computer Desktop Fetc', '2UA610K02T'),
('123', 'M00876', 'Computer Desktop Fetc', '2UA610K03S'),
('123', 'M00877', 'Computer Desktop Fetc', '2UA610K035'),
('123', 'M00878', 'Computer Desktop Fetc', '2UA610K02C'),
('123', 'M00879', 'Computer Desktop Fetc', '2UA610K05L'),
('123', 'M00880', 'Computer Desktop Fetc', '2UA610K01Z'),
('123', 'M00881', 'Computer Desktop Fetc', '2UA610K057'),
('123', 'M00882', 'Computer Desktop Fetc', '2UA610K04N'),
('123', 'M00883', 'Computer Desktop Fetc', '2UA610K05F'),
('123', 'M00884', 'Computer Desktop Fetc', '2UA610K01N'),
('123', 'M00885', 'Computer Desktop Fetc', '2UA610K041'),
('123', 'M00886', 'Computer Desktop Fetc', '2UA610K027'),
('123', 'M00887', 'Computer Desktop Fetc', '2UA610K03N'),
('123', 'M00888', 'Computer Desktop Fetc', '2UA610K03M'),
('123', 'M00889', 'Computer Desktop Fetc', '2UA610K03W'),
('123', 'M00890', 'Computer Desktop Fetc', '2UA610K03M'),
('123', 'M00891', 'Computer Desktop Fetc', '2UA610K04J'),
('123', 'M00892', 'Computer Desktop Fetc', '2UA610K047'),
('123', 'M00893', 'Computer Desktop Fetc', '2UA610K056'),
('123', 'M00894', 'Computer Desktop Fetc', '2UA610K02G'),
('123', 'M00896', 'Computer Desktop Fetc', '2UA610K02Q'),
('123', 'M00897', 'Computer Desktop Fetc', '2UA610K04G'),
('123', 'M00898', 'Computer Desktop Fetc', '2UA610K01P'),
('123', 'M00899', 'Computer Desktop Fetc', '2UA610K05G'),
('123', 'M00900', 'Computer Desktop Fetc', '2UA610K04F'),
('123', 'M00901', 'Computer Desktop Fetc', '2UA610K05D'),
('123', 'M60726', 'Computer Dc5800', 'MXL844082M'),
('135', 'G42593', 'Computer Desktop D530', 'USV34404BR'),
('135', 'G42599', 'Computer Desktop D530', 'USV34404BM'),
('135', 'G42600', 'Computer Desktop D530', 'USV34404BV'),
('135', 'G42609', 'Computer Desktop D530', 'USV34404BK'),
('135', 'J72662', 'Computer Dc7100', '2UA5020KJ0'),
('135', 'J72667', 'Computer Dc7100', '2UA5020KHV'),
('135', 'J72670', 'Computer Dc7100', '2UA5020KHD'),
('135', 'J72674', 'Computer Dc7100', '2UA5020KHN'),
('135', 'J72678', 'Computer Dc7100', '2UA5020KHS'),
('135', 'J72679', 'Computer Dc7100', '2UA5020KHH'),
('135', 'J72680', 'Computer Dc7100', '2UA5020KHQ'),
('135', 'J72684', 'Computer Dc7100', '2UA5020KHM'),
('135', 'M35253', 'Computer Dc5700', '2UA7411JJ2'),
('135', 'M68784', 'Computer Dc5800', '2UA9390NF3'),
('135', 'M68785', 'Computer Dc5800', '2UA9390NF7'),
('135', 'M68786', 'Computer Dc5800', '2UA9390NDZ'),
('135', 'M68787', 'Computer Dc5800', '2UA9390NF5'),
('135', 'M68788', 'Computer Dc5800', '2UA9390NDW'),
('135', 'M68789', 'Computer Dc5800', '2UA9390NFG'),
('135', 'M68790', 'Computer Dc5800', '2UA9390NFD'),
('135', 'M68791', 'Computer Dc5800', '2UA9390NF0'),
('135', 'M68792', 'Computer Dc5800', '2UA9390NFF'),
('135', 'M68793', 'Computer Dc5800', '2UA9390NF8'),
('135', 'M68794', 'Computer Dc5800', '2UA9390NDP'),
('135', 'M68795', 'Computer Dc5800', '2UA9390NDR'),
('135', 'M68796', 'Computer Dc5800', '2UA9390NDY'),
('135', 'M68797', 'Computer Dc5800', '2UA9390NF6'),
('135', 'M68798', 'Computer Dc5800', '2UA9390NFC'),
('135', 'M68799', 'Computer Dc5800', '2UA9390NFB'),
('135', 'M68800', 'Computer Dc5800', '2UA9390NDQ'),
('135', 'M68801', 'Computer Dc5800', '2UA9390NDV'),
('135', 'M68802', 'Computer Dc5800', '2UA9390NFH'),
('135', 'M68803', 'Computer Dc5800', '2UA9390NF4'),
('135', 'M68804', 'Computer Dc5800', '2UA9390NDX'),
('135', 'M68805', 'Computer Dc5800', '2UA9390NF9'),
('135', 'M68806', 'Computer Dc5800', '2UA9390NDT'),
('135', 'M68807', 'Computer Dc5800', '2UA9390NDS'),
('135', 'M68808', 'Computer Dc5800', '2UA9390NF2'),
('135', 'M68809', 'Computer Dc5800', '2UA9390NF1'),
('410', 'M35295', 'Computer Dc5700', '2UA7411JK8'),
('208', 'G78966', 'Server Ml370', 'USE804N5P6');
 
there is no need to split out bits of the functionality into different files. all can be done in one, provided that you judiciously kill the script before the wrong stuff gets sent.

here is some working code for you. have a look at it and also the other similar thread so you can see how it all fits together. it's very simple when it all clicks.

Code:
[COLOR=#990000 ]<?[/color]PHP 
[i][COLOR=#9A1900 ]//database settings[/color][/i]
[COLOR=#009900 ]$dbhost[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]'localhost'[/color][COLOR=#990000 ];[/color]
[COLOR=#009900 ]$dbuser[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]
[COLOR=#009900 ]$dbpass[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]
[COLOR=#009900 ]$dbname[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]'help_desk'[/color][COLOR=#990000 ];[/color]

[i][COLOR=#9A1900 ]//Connect to MySQL Server[/color][/i]
[COLOR=#009900 ]$connection[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_connect[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$dbhost[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$dbuser[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$dbpass[/color][COLOR=#990000 ]);[/color]

[i][COLOR=#9A1900 ]//Select Database[/color][/i]
[COLOR=#009900 ]$db[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_select_db[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$dbname[/color][COLOR=#990000 ])[/color] [b][COLOR=#0000FF ]or[/color][/b] [b][COLOR=#0000FF ]die[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]mysql_error[/color][/b][COLOR=#990000 ]());[/color]

[b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]getRoomEquipment[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$room[/color][COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab][COLOR=#009900 ]$sql[/color][COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"SELECT bpi_tag FROM `hesk_equipment` WHERE `room`='"[/color] [COLOR=#990000 ].[/color] [b][COLOR=#000000 ]mysql_real_escape_string[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$room[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ].[/color] [COLOR=#FF0000 ]"'"[/color][COLOR=#990000 ];[/color]
[tab][COLOR=#009900 ]$result[/color][COLOR=#990000 ]=[/color][COLOR=#009900 ]@mysql_query[/color][COLOR=#990000 ]([/color][COLOR=#009900 ]$sql[/color][COLOR=#990000 ]);[/color]
[tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$result[/color] [COLOR=#990000 ]===[/color] false[COLOR=#990000 ])[/color] [b][COLOR=#0000FF ]return[/color][/b] false[COLOR=#990000 ];[/color]
[tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]();[/color]
[tab][b][COLOR=#0000FF ]while[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$result[/color][COLOR=#990000 ])):[/color]
[tab][tab][COLOR=#009900 ]$return[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'text'[/color][COLOR=#990000 ]=>[/color][b][COLOR=#000000 ]htmlspecialchars[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'bpi_tag'[/color][COLOR=#990000 ]]),[/color] [COLOR=#FF0000 ]'value'[/color][COLOR=#990000 ]=>[/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'bpi_tag'[/color][COLOR=#990000 ]]);[/color]
[tab][b][COLOR=#0000FF ]endwhile[/color][/b][COLOR=#990000 ];[/color]
[tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#009900 ]$return[/color][COLOR=#990000 ];[/color]
[COLOR=#FF0000 ]}[/color]

[b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#0000FF ]isset[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$_REQUEST[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'action'[/color][COLOR=#990000 ]])[/color] [COLOR=#990000 ]&&[/color] [COLOR=#009900 ]$_REQUEST[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'action'[/color][COLOR=#990000 ]][/color] [COLOR=#990000 ]==[/color] [COLOR=#FF0000 ]'getEquipment'[/color][COLOR=#990000 ]):[/color]
[tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ](![/color][b][COLOR=#0000FF ]isset[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$_REQUEST[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'room'[/color][COLOR=#990000 ]])):[/color]
[tab][tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'result'[/color][COLOR=#990000 ]=>[/color]false[COLOR=#990000 ]);[/color]
[tab][b][COLOR=#0000FF ]else[/color][/b][COLOR=#990000 ]:[/color]
[tab][tab][COLOR=#009900 ]$equipment[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]getRoomEquipment[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$_REQUEST[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'room'[/color][COLOR=#990000 ]]);[/color]
[tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$equipment[/color] [COLOR=#990000 ]===[/color] false[COLOR=#990000 ]):[/color]
[tab][tab][tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'result'[/color][COLOR=#990000 ]=>[/color]false[COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'error'[/color][COLOR=#990000 ]=>[/color][b][COLOR=#000000 ]mysql_error[/color][/b][COLOR=#990000 ]());[/color]
[tab][tab][b][COLOR=#0000FF ]else[/color][/b][COLOR=#990000 ]:[/color]
[tab][tab][tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'result'[/color][COLOR=#990000 ]=>[/color][COLOR=#FF0000 ]'ok'[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'data'[/color][COLOR=#990000 ]=>[/color][COLOR=#009900 ]$equipment[/color][COLOR=#990000 ]);[/color]
[tab][tab][b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
[tab][b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
[tab][b][COLOR=#0000FF ]echo[/color][/b] [b][COLOR=#000000 ]json_encode[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$return[/color][COLOR=#990000 ]);[/color]
[tab][b][COLOR=#0000FF ]die[/color][/b][COLOR=#990000 ];[/color]
[b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
[COLOR=#009900 ]$sql[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"SELECT DISTINCT `room` FROM `hesk_clients` WHERE `room` > ''"[/color][COLOR=#990000 ];[/color]
[COLOR=#009900 ]$result[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_query[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$sql[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$connection[/color][COLOR=#990000 ])[/color] [b][COLOR=#0000FF ]or[/color][/b] [b][COLOR=#0000FF ]die[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]mysql_error[/color][/b][COLOR=#990000 ]());[/color]

[COLOR=#990000 ]?>[/color]
[COLOR=#990000 ]<![/color]DOCTYPE html PUBLIC [COLOR=#FF0000 ]"-//W3C//DTD XHTML 1.0 Transitional//EN"[/color] [COLOR=#FF0000 ]"[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"[/URL][/color][COLOR=#990000 ]>[/color]
[COLOR=#990000 ]<[/color]html[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]head[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]title[COLOR=#990000 ]>[/color]Combobox Test[COLOR=#990000 ]</[/color]title[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]meta http[COLOR=#990000 ]-[/color]equiv[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"Content-Type"[/color] content[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"text/html; charset=utf-8"[/color][COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]script type[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"text/javascript"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]populate_dropdwn2[/color][/b][COLOR=#990000 ]([/color]elem1[COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][b][COLOR=#0000FF ]var[/color][/b] xmlhttp[COLOR=#990000 ];[/color]   
[tab][tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color]window[COLOR=#990000 ].[/color]XMLHttpRequest[COLOR=#990000 ])[/color] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab]xmlhttp [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]new[/color][/b] [b][COLOR=#000000 ]XMLHttpRequest[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab][b][COLOR=#0000FF ]else[/color][/b] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab]xmlhttp [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]new[/color][/b] [b][COLOR=#000000 ]ActiveXObject[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]"Microsoft.XMLHTTP"[/color][COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab]xmlhttp[COLOR=#990000 ].[/color]onreadystatechange[COLOR=#990000 ]=[/color][b][COLOR=#0000FF ]function[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color]xmlhttp[COLOR=#990000 ].[/color]readyState [COLOR=#990000 ]==[/color] [COLOR=#993399 ]4[/color] [COLOR=#990000 ]&&[/color] xmlhttp[COLOR=#990000 ].[/color]status [COLOR=#990000 ]==[/color] [COLOR=#993399 ]200[/color][COLOR=#990000 ])[/color] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][tab][b][COLOR=#0000FF ]var[/color][/b] results[COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]eval[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'('[/color] [COLOR=#990000 ]+[/color] xmlhttp[COLOR=#990000 ].[/color]responseText [COLOR=#990000 ]+[/color] [COLOR=#FF0000 ]')'[/color] [COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab][tab]
[tab][tab][tab][tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color]results[COLOR=#990000 ].[/color]result [COLOR=#990000 ]==[/color] [COLOR=#FF0000 ]'ok'[/color][COLOR=#990000 ])[/color] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][tab][tab][b][COLOR=#0000FF ]var[/color][/b] elem [COLOR=#990000 ]=[/color] document[COLOR=#990000 ].[/color][b][COLOR=#000000 ]getElementById[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'asset_no'[/color][COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab][tab][tab]
[tab][tab][tab][tab][tab][tab][tab][i][COLOR=#9A1900 ]//delete all current options[/color][/i]
[tab][tab][tab][tab][tab][tab][tab][b][COLOR=#0000FF ]for[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#0000FF ]var[/color][/b] i [COLOR=#990000 ]=[/color] elem[COLOR=#990000 ].[/color]length[COLOR=#990000 ]-[/color][COLOR=#993399 ]1[/color][COLOR=#990000 ];[/color] i [COLOR=#990000 ]>=[/color] [COLOR=#993399 ]1[/color][COLOR=#990000 ];[/color] i[COLOR=#990000 ]--[/color] [COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][tab][tab][tab]elem[COLOR=#990000 ].[/color]options[COLOR=#990000 ][[/color]i[COLOR=#990000 ]][/color] [COLOR=#990000 ]=[/color] null[COLOR=#990000 ];[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab][tab][tab][tab]
[tab][tab][tab][tab][tab][tab][tab][i][COLOR=#9A1900 ]//now add in new options[/color][/i]
[tab][tab][tab][tab][tab][tab][tab][b][COLOR=#0000FF ]for[/color][/b] [COLOR=#990000 ]([/color]i [COLOR=#990000 ]=[/color] [COLOR=#993399 ]0[/color][COLOR=#990000 ];[/color] i [COLOR=#990000 ]<[/color] results[COLOR=#990000 ].[/color]data[COLOR=#990000 ].[/color]length[COLOR=#990000 ];[/color] i[COLOR=#990000 ]++[/color] [COLOR=#990000 ])[/color] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][tab][tab][tab]elem[COLOR=#990000 ].[/color]options[COLOR=#990000 ][[/color]elem[COLOR=#990000 ].[/color]length[COLOR=#990000 ]][/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]new[/color][/b] [b][COLOR=#000000 ]Option[/color][/b][COLOR=#990000 ]([/color]results[COLOR=#990000 ].[/color]data[COLOR=#990000 ][[/color]i[COLOR=#990000 ]].[/color]text[COLOR=#990000 ],[/color] results[COLOR=#990000 ].[/color]data[COLOR=#990000 ][[/color]i[COLOR=#990000 ]].[/color]value[COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#FF0000 ]}[/color] [b][COLOR=#0000FF ]else[/color][/b] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][tab][tab][b][COLOR=#000000 ]alert[/color][/b] [COLOR=#990000 ]([/color]results[COLOR=#990000 ].[/color]error[COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab][tab][COLOR=#FF0000 ]}[/color][tab][tab]   
[tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][tab][b][COLOR=#0000FF ]var[/color][/b] si [COLOR=#990000 ]=[/color] elem1[COLOR=#990000 ].[/color]selectedIndex[COLOR=#990000 ];[/color]
[tab][tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color]si [COLOR=#990000 ]>[/color] [COLOR=#993399 ]0[/color][COLOR=#990000 ])[/color] [COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][tab][b][COLOR=#0000FF ]var[/color][/b] val [COLOR=#990000 ]=[/color] elem1[COLOR=#990000 ][[/color]si[COLOR=#990000 ]].[/color]value[COLOR=#990000 ];[/color]
[tab][tab][tab][tab][tab]xmlhttp[COLOR=#990000 ].[/color][b][COLOR=#000000 ]open[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]"POST"[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]"?action=getEquipment&room="[/color] [COLOR=#990000 ]+[/color] [b][COLOR=#000000 ]encodeURIComponent[/color][/b][COLOR=#990000 ]([/color]val[COLOR=#990000 ]),[/color] true[COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab]xmlhttp[COLOR=#990000 ].[/color][b][COLOR=#000000 ]setRequestHeader[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]"Content-type"[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]"application/x-www-form-urlencoded"[/color][COLOR=#990000 ]);[/color]
[tab][tab][tab][tab][tab]xmlhttp[COLOR=#990000 ].[/color][b][COLOR=#000000 ]send[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab][COLOR=#FF0000 ]}[/color]
[tab][tab][tab]window[COLOR=#990000 ].[/color]onload [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]function[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][tab][tab][b][COLOR=#000000 ]populate_dropdwn2[/color][/b][COLOR=#990000 ]([/color]document[COLOR=#990000 ].[/color][b][COLOR=#000000 ]getElementById[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'room'[/color][COLOR=#990000 ]));[/color]
[tab][tab][tab][COLOR=#FF0000 ]}[/color][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#990000 ]</[/color]script[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]</[/color]head[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]<[/color]body[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]<[/color]form method[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"post"[/color] action[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]""[/color] name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"form"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]<[/color]table border[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"0"[/color] cellspacing[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"0"[/color] cellpadding[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"0"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]<[/color]tr[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]td width[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"15%"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab]Room
[tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]td width[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"30%"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]select name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"room"[/color] id[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"room"[/color] [b][COLOR=#0000FF ]class[/color][/b][COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"parent"[/color] onchange[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"populate_dropdwn2(this);"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]option value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]""[/color] selected[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"selected"[/color][COLOR=#990000 ]>--[/color]Select Room[COLOR=#990000 ]--</[/color]option[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<?php[/color]  [b][COLOR=#0000FF ]while[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$rows[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$result[/color][COLOR=#990000 ])):[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]option value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"<?php echo $rows['room'];?>"[/color][COLOR=#990000 ]><?php[/color] [b][COLOR=#0000FF ]echo[/color][/b] [b][COLOR=#000000 ]htmlspecialchars[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$rows[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'room'[/color][COLOR=#990000 ]]);[/color] [COLOR=#990000 ]?></[/color]option[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<?php[/color] [b][COLOR=#0000FF ]endwhile[/color][/b][COLOR=#990000 ];[/color] [COLOR=#990000 ]?>[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]select[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]</[/color]tr[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]<[/color]tr[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]br[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]</[/color]tr[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]<[/color]tr style[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]""[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab]Asset Number
[tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]select name[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"asset_no"[/color] [b][COLOR=#0000FF ]class[/color][/b][COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"child"[/color] id[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"asset_no"[/color][COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][tab][COLOR=#990000 ]<[/color]option value[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]""[/color] selected[COLOR=#990000 ]=[/color][COLOR=#FF0000 ]"selected"[/color][COLOR=#990000 ]>--[/color]Select Asset[COLOR=#990000 ]--</[/color]option[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]select[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][tab][COLOR=#990000 ]</[/color]td[COLOR=#990000 ]>[/color]
[tab][tab][tab][tab][COLOR=#990000 ]</[/color]tr[COLOR=#990000 ]>[/color]
[tab][tab][tab][COLOR=#990000 ]</[/color]table[COLOR=#990000 ]>[/color]
[tab][tab][COLOR=#990000 ]</[/color]form[COLOR=#990000 ]>[/color]
[tab][COLOR=#990000 ]</[/color]body[COLOR=#990000 ]>[/color]
[COLOR=#990000 ]</[/color]html[COLOR=#990000 ]>[/color]
 
Jpadie's example is a good one however I was suggesting you separate the bits so it was easier to understand what needed to be done.

In any case, your new separate PHP script if that's all there is will certainly throw errors as you have not included any database connection or actual db selection code.

Remember the code must work on its own completely and output to screen. If you call that up with a value in the query string does your code work I wouldn't think think so.


Also I have created a separate file called combo_look_up.php with the PHP code that was working in terms of populating a combo box before I separated it but was not being triggered by the first combo box. I don't see how to trigger the external PHP code to populate the second combo box
It was not being triggered because there's nothing to trigger. Your function is empty. Remember things don't happen magically they have a sequence of steps that need to happen before your result occurs. The onChange event triggers a function which calls the Ajax function, which in turn calls up the PHP script which returns the values which then JS can use. All the ajax needs to go inside the populate function so it gets run.

Additionally this line in the Ajax section is what sets the file to execute. You kept it as it was, but it needs to be changed to whatever your script name is.,

Code:
  xmlhttp.open("POST","[red]script.php[/red]",true);


This will call the specified PHP file and run it, and retrieve whatever is output to screen.

With that retrieved you can know use Javascript to populate the second dropdown.

There's several ways to then populate the dropdown. You can either dynamically build it using the appropriate JS options.add function for the dropdowns or you can simply use the innerHTML property of whatever element will hold your second dropdown.



----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
Phil said:
however I was suggesting you separate the bits so it was easier to understand what needed to be done.
oops. didn't read the whole thread. My bad. interesting that this is a kind of a repetition of the other recent thread though ?
 
nteresting that this is a kind of a repetition of the other recent thread though ?
Yup very much so.

----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
@ Vacunita and Jpadie Thanks so much for your help. And sorry for being slow on the uptake.

Jpadie I did also look at the w3schools example before coming here as well of many pre-made examples on the web which did not work. I tend to learn better when I have to "struggle" with a new concept for me in programing. I am appreciative of the code you produced and it would have been an easy fix for me but while it does work it does not load the correct asset number for the room selected. My intention for the final working code is to have the basic room number that the employee is housed in to be loaded on the start of the script. These employees are teachers and from time to time they report equipment in other rooms. So with that in mind they will have to be able to reselect the room number and have the assets for that room load in the second combo box. This does not happen in the code you posted, it keeps the original asset numbers that were loaded when the script was first loaded.

Vacunita,

If you call that up with a value in the query string does your code work I wouldn't think think so.


You are right, now it does.... my misunderstanding.

Additionally this line in the Ajax section is what sets the file to execute. You kept it as it was, but it needs to be changed to whatever your script name is.,

xmlhttp.open("POST","script.php",true);

Fixed and again missed by me. Now both scripts output to the screen. The second combo box is not loading... So I "think" I am ready for a JS lesson....

There's several ways to then populate the dropdown. You can either dynamically build it using the appropriate JS options.add function for the dropdowns or you can simply use the innerHTML property of whatever element will hold your second dropdown.

Can you show examples of both?

Thanks I have learned much so far.

d


New external script for second combo box

Code:
<?php
//database settings
require('db.php');

//Connect to MySQL Server
$connection = mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
$db = mysql_select_db($dbname) or die(mysql_error());

$sql = "SELECT DISTINCT `room` FROM `hesk_clients` WHERE `room` > ''";
$result = mysql_query($sql, $connection) or die( mysql_error() );
$value=$_GET['ddvalue'];
        
$sql="SELECT * FROM `hesk_equipment` WHERE `room`='" . $value . "'";
$result=mysql_query($sql);                
while ($rows = mysql_fetch_assoc(@$result))
{?>
<option value="<?php echo $rows['bpi_tag'];?>"><?php echo $rows['bpi_tag'];?></option>
<?php
}?>
 
Opps... should have been this.

Code:
<?php
//database settings
require('db.php');

//Connect to MySQL Server
$connection = mysql_connect($dbhost, $dbuser, $dbpass);

//Select Database
$db = mysql_select_db($dbname) or die(mysql_error());

$value=$_GET['ddvalue'];
        
$sql="SELECT * FROM `hesk_equipment` WHERE `room`='" . $value . "'";
$result=mysql_query($sql);                
while ($rows = mysql_fetch_assoc(@$result))
{?>
		<option value="<?php echo $rows['bpi_tag'];?>"><?php echo $rows['bpi_tag'];?></option>
<?php
}?>
 
O.k now comes the interesting bit the actual building of the dropdown n JS with the data received from our server-side script.

Certain browsers will let you do something like :

Code:
var my dd=document.getElementById('mydropdownID');
dd.innerHTML="<option...>...</option>...";

However IE will not, it simply will not construct the dropdown from that, so rather than have the PHP output the actual <option> HTML we need it to output the data in a usable format since JS is going to take that and construct the new dropdown.


I suggest you have your PHP output like this:

Code:
while ($rows = mysql_fetch_assoc(@$result))
{
echo $rows['bpi_tag'] . ',';

}

This should provide a nice comma separated list of values for the JS to use.

Now in our JS function:
Code:
  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

//You can use the responseText property of your ajax variable to get the output from your server side script.      

var results=xmlhttp.responseText;    

//From there you can do with the result string whatever you want. Stick it in a DIV, or continue to parse it out and work with it if you require.

[green]//We'll put our parsing Js code here to build our new DD[/green]

[green]//Get our reference object to our dropdown[/green]
var dd=document.getElementById('asset_no'); 
[green]//Clear any options that may be present in it before adding the new ones. [/green]
dd.options.length=0;

[green]//take our results and split them at the commas to get an array we can loop over. [/green]
var resultsArray=results.split(",");
[green]//Lop over array and create our options from it[/green]
for(var i=0;i<=resultsArray.length-2;i++){
var newOption=document.createElement('option');
newOption.value=resultsArray[i];
newOption.text=resultsArray[i];
[green]//Add the newly created option to our dropdown[/green]
dd.options.add(newOption);
}

}

----------------------------------
Phil AKA Vacunita
----------------------------------
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.

Behind the Web, Tips and Tricks for Web Development.
 
oh well. it seems to give the right data for me. if it does not for you then there must be an issue with the sql.
 
@jpadie try reselecting room 410 there should only be one asset number associated with that room. Try it you will find that it returns the same list of asset numbers no matter what room you select.

@Vacunita

I suggest you have your PHP output like this:

This should provide a nice comma separated list of values for the JS to use

It does but there is a trailing comma, not sure if that will be an issue.

Now in our JS function:

Couple of questions. I am assuming that this script lives within the script tags in the file referenced above. Also what triggers it? Lastly can this code as well as the AJAX setup so it is generic? Just thinking ahead incase I find this useful (pretty sure I will). Thanks

d



 
that is what I receive. Just one item.

tested in chrome, firefox and safari. not tested in MS browsers.

Screen%20Shot%202011-09-05%20at%205.41.53%20PM.png


I do not code for IE but perhaps if you change the option removal line (66 on my editor) to the following it will work better

Code:
elem[COLOR=#990000 ].[/color][b][COLOR=#000000 ]remove[/color][/b][COLOR=#990000 ]([/color]i[COLOR=#990000 ]);[/color]

to clean the trailing comma in Phil's suggestion, within php instead of
Code:
[b][COLOR=#0000FF ]while[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$rows[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color]@[COLOR=#009900 ]$result[/color][COLOR=#990000 ]))[/color]
[COLOR=#FF0000 ]{[/color]
[b][COLOR=#0000FF ]echo[/color][/b] [COLOR=#009900 ]$rows[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'bpi_tag'[/color][COLOR=#990000 ]][/color] [COLOR=#990000 ].[/color] [COLOR=#FF0000 ]','[/color][COLOR=#990000 ];[/color]

[COLOR=#FF0000 ]}[/color]
use the following

Code:
[COLOR=#009900 ]$output[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]();[/color]
[b][COLOR=#0000FF ]while[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$rows[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color]@[COLOR=#009900 ]$result[/color][COLOR=#990000 ]))[/color][COLOR=#FF0000 ]{[/color]
[COLOR=#990000 ][tab][/color][COLOR=#009900 ]$output[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$rows[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'bpi_tag'[/color][COLOR=#990000 ]];[/color]
[COLOR=#FF0000 ]}[/color]
[b][COLOR=#0000FF ]echo[/color][/b] [b][COLOR=#000000 ]implode[/color][/b] [COLOR=#990000 ]([/color][COLOR=#FF0000 ]','[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$output[/color][COLOR=#990000 ]);[/color]

or
Code:
[COLOR=#009900 ]$output[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]''[/color][COLOR=#990000 ];[/color]
[b][COLOR=#0000FF ]while[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$rows[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]mysql_fetch_assoc[/color][/b][COLOR=#990000 ]([/color]@[COLOR=#009900 ]$result[/color][COLOR=#990000 ]))[/color][COLOR=#FF0000 ]{[/color]
[tab][COLOR=#009900 ]$output[/color] [COLOR=#990000 ].=[/color] [COLOR=#009900 ]$rows[/color][COLOR=#990000 ][[/color][COLOR=#FF0000 ]'bpi_tag'[/color][COLOR=#990000 ]][/color] [COLOR=#990000 ].[/color] [COLOR=#FF0000 ]','[/color][COLOR=#990000 ];[/color]
[COLOR=#FF0000 ]}[/color]
[b][COLOR=#0000FF ]echo[/color][/b] [b][COLOR=#000000 ]rtrim[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$output[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]','[/color][COLOR=#990000 ]);[/color]

or you can test for emptiness within javascript. but that is best asked and resolved in the javascript forum.

if you are thinking of developing your web apps into fully fledged ajax applications, consider getting to grips with a js framework like jQuery.
 
I do not code for IE

This is being run and tested in a IE environment. No other options.

if you are thinking of developing your web apps into fully fledged ajax applications, consider getting to grips with a js framework like jQuery.

That is why I am here.... :cool: Generally I don't program, I am a system admin.

Thanks again for your help though.

d
 
then use
Code:
elem.remove(i);
as suggested.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top