Jump to content


Photo

Select Box with database


  • Please log in to reply
3 replies to this topic

#1 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 21 July 2006 - 07:18 PM

Not sure where to put this as its javascript and php related. So if it needs to be moved im sorry for the inconvience.

Anyway. I have a script that will make a select combo box. But i need to update it from values from different select boxes i have. Basically a form.

So depending what is selected ill need to build a query to get a new combo box. Although the values will go into the same select box every time. It will clear out the select box first then add the new items in there.

My question is how do i populate the select box i already have or how can i go about coverting a server side variable from php and putting it so javascript can read it then just use javascript to populate the box.

call on this to make a new one
$ComboBox = new Combo_Box($name,$table_name,$order_by,$asc_desc,$style,$id_value,$idname);

<?php
require_once("./combo_box.conf.php");
class Combo_Box {
function Combo_Box($cb_name,$table_name,$order_by="",$asc="",$css_class="",$id="",$idname) {
if (sizeof($table_name) > '0'){
$i='0';
foreach ($table_name AS $tablename)
{
	if($i == "0")
	{
	  $sql .= "SELECT indexi,code FROM ".$tablename;
	}
	else
	{
		$sql .= " UNION SELECT indexi,code FROM ".$tablename;
	}
	$i++;
	
}
}
else
{
	echo "Specify a database";
}

		if ($table_name) {
			if($id) {
				$disable = " disabled ";
			}
			if ($order_by) {
				$order_by = " ORDER BY ".$order_by." ".$asc;
			}
			$sql .= $order_by;
			
			$result = mysql_query($sql) or die("Error with query (".$sql."): ".mysql_error());
			$show_Combo_Box = ""
			."<SELECT name=\"".$cb_name."\" id=\"".$idname."\" class=\"".$css_class."\" ".$disable."> \n"
			."<OPTION value=\"0\">Select</OPTION>\n";
			WHILE ($row  = mysql_fetch_array($result)) {
				$selection = "";
				if($id){
					if($row['indexi'] == $id){
						$selection = " selected ";
					}
				}
				$show_Combo_Box .= ""
				."<OPTION value=\"".$row['indexi']."\" ".$selection.">"
				.$row['indexi']." (".$row['code'].") "
				."</OPTION> \n";
			} // End WHILE
			$show_Combo_Box .= ""
			."</SELECT>\n";
			mysql_free_result($result);
			echo $show_Combo_Box;
		} // End if ($table_name)
	} // End function Combo_Box
} // End class Combo_Box


?>

as you can see it makes a new select box from the databases from an array. Then it just echos it and makes the select box.

My question is how can i go about clearing which i can do that populating it.

<script type="text/javascript">
function clearbox()
{
items = document.getElementById('items');
while (items.options.length > 0) {
    items.options[0] = null;
}
populate();
}
</SCRIPT>

I called on this to clear it which works fine. Now to re-populate i cant figure out. I was thinking if it was possible to maybe modify the code i have to make a new select box. Change it so it does the document.id("items") is what the box is called for me. and then loop through like above except somehow start adding options to it. Or is there some easier way. Thanks in advanced.

#2 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 21 July 2006 - 07:37 PM

If you want PHP to get new values based on what you select in the first box, you'll have to submit the form with the value of that box and run a new query for the new values.

If you want JavaScript to populate the new values, you will need to run a query in advance for each set of values the combo box can hold.



#3 kungfu71186

kungfu71186
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 21 July 2006 - 07:45 PM

If you want PHP to get new values based on what you select in the first box, you'll have to submit the form with the value of that box and run a new query for the new values.

If you want JavaScript to populate the new values, you will need to run a query in advance for each set of values the combo box can hold.



i cant just query new values? I have a button to update it and it calls on a function but i dont want it to reload the page everytime. There are about 50,000 combinations. But right now i have about 50 tables or more and there will be more to come. So thats a lot of querys if i do it before and shove them into an array or something and they have to be seperated out so i know what values to pull. Is it possible to submit the form without having it reload or anything?

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 28 July 2006 - 02:02 PM

Because your combo box has to query a database for it values as the user selection changes, this is all done server-side. This means you have to send that change to the server for a new query, which will mean a page reload.

However, you can make your script appear to not reload using AJAX.

You can read up on it here:

AJAX Freaks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users