Jump to content

Select Box with database


kungfu71186

Recommended Posts

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
[code]$ComboBox = new Combo_Box($name,$table_name,$order_by,$asc_desc,$style,$id_value,$idname);[/code]

[code]<?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


?>[/code]

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.

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

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

[quote author=HeyRay2 link=topic=101419.msg401380#msg401380 date=1153510676]
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.


[/quote]

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?
Link to comment
Share on other sites

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 [B]appear[/B] to not reload using AJAX.

You can read up on it here:

[url=http://www.ajaxfreaks.com]AJAX Freaks[/url]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.