RE: [PHP] PHP, JavaScript and populating DropDowns
| From: | Creative Solutions New Media | Date: | Thu, 31 Jul 2003 03:27:18 +0000 |
| Subject: | RE: [PHP] PHP, JavaScript and populating DropDowns | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-157651@lists.php.net to get a copy of this message | ||
Thanks very much John. It looks great.
Tim Winters
Manager, Creative Development
Sampling Technologies Incorporated (STI)
webmaster@samplingtechnologies.com
csnm@samplingtechnologies.com
W: 902 450 5500
C: 902 430 8498
-----Original Message-----
From: John Manko [mailto:xerid@adelphia.net]
Sent: July 29, 2003 2:49 PM
To: csnm@ns.sympatico.ca
Cc: Php-General@Lists. Php. Net
Subject: Re: [PHP] PHP, JavaScript and populating DropDowns
assuming you have a table with colors, and each color has car associated
with it (yes, there might be dup color entries).
This should work (check spellings and case, etc..etc...)
$query="SELECT * from colors";
$results = mysql_query($query) or die ("DB ERROR:" . mysql_error());
$num = mysql_numrows($results);
$i=0;
echo "<script language=javascript>\n<!--\n";
while ($i < $num) {
echo "\n ca[$i] = new Array();";
echo "\n ca[$i][0] = \"" . mysql_result($results,$i,"CarID") .
"\";";
echo "\n ca[$i][1] = \"" . mysql_result($results,$i,"ColorID") .
"\";";
echo "\n ca[$i][2] = \"" . mysql_result($results,$i,"ColorName") .
"\";\n";
++$i;
}
echo "
function changeColor(ns)
{ var SelectColor, NumEntries, i;
for (i = document.form1.selectcolor.length; i > 0; i--)
{
document.addform.selectcolor.options[i-1] = null;
}
document.form1.selectcolor.options[0] = new Option("-- Select Color
--",0);
len = 0;
if (ns != "0")
{
NumEntries = ca.length
for (i = 0; i < NumEntries; i++)
{
if(ca[i][0] == ns){
len++;
document.form1.selectcolor.options[len] = new
Option(ca[i][2],ca[i][1]);
}
}
}
document.form1.selectcolor.selectedIndex = 0;
}
";
echo "\n//-->\n</script>\n";
this in later in the page...
$query="SELECT * from car";
$results = mysql_query($query) or die("<br>ERROR - Please Contact Site
Administrator<br>");
$num = mysql_numrows($results);
$i=0;
echo "<select onchange='changeColor(this.value);'
name='selectcar'><option value='0'>Select Car</option>";
while ($i < $num) {
$id=mysql_result($results,$i,"CarID");
$name=mysql_result($results,$i,"CarName");
echo "<option value='$id'>$name</option>";
++$i;
}
echo "</select>";
echo "<select name="selectcolor"><option value='0'>-- Select
Color
--</option></select>";
----------------------------------------------------------------------------
-----------------------
Creative Solutions New Media wrote:
>Hello,
>
>I am using mySQL and PHP to pull some data into my page.
>
>I have 2 dropdown boxes.
>
>As an example lets say dropbox1 has a list of cars and dropbox2 has a list
>of colours.
>
>Here's the thing. The colours for each car vary so I won't know what
>colours to populate the 2nd dropbox with until a car is chosen. I really
>want this all contained on 1 page.
>
>Is there a standard way of dealing with this? The only thing I can think
of
>is to reload the page once the make of car is selected which would give me
>the opportunity to populate the 2nd drop box at that time but that doesn't
>seem like a good solution.
>
>I don't know much in the way of JS but is there a way to do what I need
that
>way?
>
>Thanks.
>
>Tim Winters
>Manager, Creative Development
>Sampling Technologies Incorporated (STI)
>webmaster@samplingtechnologies.com
>csnm@samplingtechnologies.com
>W: 902 450 5500
>C: 902 430 8498
>
>
>
>
>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php