scottlowe
05-07-2008, 04:51 AM
I'll admit right up front that I know just enough about development to be dangerous, so my code is pretty ugly. I have a lot of debug stuff in it so that I can try to figure out what the heck is going on. I really do appreciate any help someone might be able to provide.
I'm helping my sister develop a new web site for a local tire company and they want a tire finder. I've loaded the database (just a simple flat file into a single table since they want regular updates that are exported from their POS system) with sample data.
I need to do a lookup by year, then make, then model, then submodel, automatically populating each drop down as I go. I've gotten as far as the model, but when I make a change to that dropdown, I'm getting a "can't find variable" error (Safari error console). IE (7 & 8) just show a script error.
If you want to see this, visit http://67.222.39.153/scripts/state_dropdown.php
It's so named because I found sample code to start with and simply(!) modified it.
There are two files that make this thing up: state.php and state_dropdown.php. I've listed both below: I've also zipped them up so they're easier to download and read: visit: http://kostdev.com/darnscripts.zip to download.
state.php:
<?php
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=tis-620");
$data = $_GET['data'];
$val = $_GET['val'];
$val2 = $_GET['val2'];
$val3 = $_GET['val3'];
echo "<BR>PAGE OPEN VAL1 = " . $val . "<BR>";
echo "<BR>PAGE OPEN VAL2 = " . $val2 . "<BR>";
echo "<BR>PAGE OPEN VAL3 = " . $val3 . "<BR>";
//set database
$dbhost = "localhost";
$dbuser = "kostdevc_lookup";
$dbpass = "*****"; // user account has *select* rights only
$dbname = "kostdevc_ktdb";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
if ($data=='states')
{ // first dropdown
echo "<select name='states' onChange=\"dochange('cities', this.value)\">\n";
echo "<option value='0'>==== choose year ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT cyear FROM vehicles ORDER BY cyear DESC");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='cities')
{ // second dropdown
echo "<br>$val <-- val" ;
echo "<br>$val2 <-- val2 <br>" ;
echo "<select name='cities' onChange=\"dochangemodel('models', this.value, $val)\">\n";
echo "<option value='0'>====choose make ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT make FROM vehicles WHERE cyear = '$val' ORDER BY make");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='models')
{ // third dropdown
echo "<select name='models' onChange=\"dochangesubmodel('submodel', this.value, $val)\">\n";
// echo "<select name='models' onChange=\"dochangesubmodel('submodel', this.value, $val, $val2)\">\n";
echo "<option value='0'>====choose model ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT model FROM vehicles WHERE cyear = '$val2' AND make = '$val' ORDER BY model");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='submodel')
{ // second dropdown
echo "<br>$val <-- val" ;
echo "<br>$val2 <-- val2 <br>" ;
echo "<select name='submodel' onChange=\"docwrite()\">\n";
echo "<option value='0'>====choose make ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT make FROM vehicles WHERE cyear = '$val' ORDER BY make");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
echo "</select>\n";
?>
------------- END OF STATE.PHP
------------- STATE_DROPDOWN.PHP
<br>
This is sample Ajax DropDown Menu When you select state in first dropdown menu<br>
the second dropdown menu will change automatic and display cites in the state selection
<br><br>
<?
echo "<form name=sel>\n";
echo "Year : <font id=states><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "Make : <font id=cities><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
echo "Model : <font id=models><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
echo "Submodel : <font id=submodel><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
?>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function dochangemodel(src, val, val2) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function dochangesubmodel(src, val, val2) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function docwrite() {
document.writenln("<br>HERE!<br>");
}
/*
function dochangesubmodel(src, val, val2, val3) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2+"&val3="+val3); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
} */
window.onLoad=dochange('states', -1); // value in first dropdown
</script>
------------- END OF STATE_DROPDOWN.PHP
I sincerely appreciate the help.
Scott
I'm helping my sister develop a new web site for a local tire company and they want a tire finder. I've loaded the database (just a simple flat file into a single table since they want regular updates that are exported from their POS system) with sample data.
I need to do a lookup by year, then make, then model, then submodel, automatically populating each drop down as I go. I've gotten as far as the model, but when I make a change to that dropdown, I'm getting a "can't find variable" error (Safari error console). IE (7 & 8) just show a script error.
If you want to see this, visit http://67.222.39.153/scripts/state_dropdown.php
It's so named because I found sample code to start with and simply(!) modified it.
There are two files that make this thing up: state.php and state_dropdown.php. I've listed both below: I've also zipped them up so they're easier to download and read: visit: http://kostdev.com/darnscripts.zip to download.
state.php:
<?php
//set IE read from page only not read from cache
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");
header("content-type: application/x-javascript; charset=tis-620");
$data = $_GET['data'];
$val = $_GET['val'];
$val2 = $_GET['val2'];
$val3 = $_GET['val3'];
echo "<BR>PAGE OPEN VAL1 = " . $val . "<BR>";
echo "<BR>PAGE OPEN VAL2 = " . $val2 . "<BR>";
echo "<BR>PAGE OPEN VAL3 = " . $val3 . "<BR>";
//set database
$dbhost = "localhost";
$dbuser = "kostdevc_lookup";
$dbpass = "*****"; // user account has *select* rights only
$dbname = "kostdevc_ktdb";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server");
if ($data=='states')
{ // first dropdown
echo "<select name='states' onChange=\"dochange('cities', this.value)\">\n";
echo "<option value='0'>==== choose year ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT cyear FROM vehicles ORDER BY cyear DESC");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='cities')
{ // second dropdown
echo "<br>$val <-- val" ;
echo "<br>$val2 <-- val2 <br>" ;
echo "<select name='cities' onChange=\"dochangemodel('models', this.value, $val)\">\n";
echo "<option value='0'>====choose make ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT make FROM vehicles WHERE cyear = '$val' ORDER BY make");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='models')
{ // third dropdown
echo "<select name='models' onChange=\"dochangesubmodel('submodel', this.value, $val)\">\n";
// echo "<select name='models' onChange=\"dochangesubmodel('submodel', this.value, $val, $val2)\">\n";
echo "<option value='0'>====choose model ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT model FROM vehicles WHERE cyear = '$val2' AND make = '$val' ORDER BY model");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
else if ($data=='submodel')
{ // second dropdown
echo "<br>$val <-- val" ;
echo "<br>$val2 <-- val2 <br>" ;
echo "<select name='submodel' onChange=\"docwrite()\">\n";
echo "<option value='0'>====choose make ====</option>\n";
$result=mysql_db_query($dbname,"SELECT DISTINCT make FROM vehicles WHERE cyear = '$val' ORDER BY make");
while(list($id)=mysql_fetch_array($result))
{
echo "<option value=\"$id\" >$id</option> \n" ;
}
}
echo "</select>\n";
?>
------------- END OF STATE.PHP
------------- STATE_DROPDOWN.PHP
<br>
This is sample Ajax DropDown Menu When you select state in first dropdown menu<br>
the second dropdown menu will change automatic and display cites in the state selection
<br><br>
<?
echo "<form name=sel>\n";
echo "Year : <font id=states><select>\n";
echo "<option value='0'>============</option> \n" ;
echo "</select></font>\n";
echo "Make : <font id=cities><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
echo "Model : <font id=models><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
echo "Submodel : <font id=submodel><select>\n";
echo "<option value='0'>=== none ===</option> \n" ;
echo "</select></font>\n";
?>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function dochangemodel(src, val, val2) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function dochangesubmodel(src, val, val2) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
}
function docwrite() {
document.writenln("<br>HERE!<br>");
}
/*
function dochangesubmodel(src, val, val2, val3) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText; //retuen value
}
}
};
req.open("GET", "state.php?data="+src+"&val="+val+"&val2="+val2+"&val3="+val3); //make connection
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
req.send(null); //send value
} */
window.onLoad=dochange('states', -1); // value in first dropdown
</script>
------------- END OF STATE_DROPDOWN.PHP
I sincerely appreciate the help.
Scott