php mysql ajax chained drop down boxes

Hello, I am a little stuck with a project I have as a beginner php programmer.

I am making a 3 tier drop down box system for a tire searching engine.

By that I mean that someone looking for tires compatible with their respective cars can choose 1. Make of car 2. Model of car 3. Year of car and then subsequently be presented with the list of tires compatible with it.

So I came across a script and I have been modifying it but I cannot for the life of me figure out what I have done wrong so I am just gonna straight out copy and paste the back end of the php script. The problem is that I can choose the Brand, and Model fine, but I can’t get it to select the year…

[php]

<? 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']; $model=$_GET['model']; $dbhost = "localhost"; $dbuser = "root"; $dbpass = "pass"; $dbname = "tires"; mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server"); // the following is to populate the brands... ford, gm, honda etc.... if ($data=='brand') { echo "n"; echo "brandn"; $result=mysql_db_query($dbname,"select `id`, `brand` from brand order by `brand`"); while(list($id, $name)=mysql_fetch_array($result)){ echo "$name n" ; } // the following is to populate the respective models... eg mustang, hummer, civic... the model_id field is a number respective to the number designated to a brand... for example Ford might be 1 so the model_id for mustang would be 1, as would the model_id for shelby for example. } else if ($data=='model') { echo "n"; echo "modeln"; $result=mysql_db_query($dbname,"SELECT `id`, `model` FROM model WHERE `model_id` = '$val' ORDER BY `model` "); while(list($id, $name)=mysql_fetch_array($result)){ echo "$name n" ; } // and the following is to populate the years or submodels of the cars......year_id supposedly is the link between the model and year drop down boxes where by year_id is the model name and year is well the year. For example, year_id for a Ford mustang 2004 would be "mustang". In other words year_id links to the model field of the model table. } else if ($data=='year') { echo "n"; echo "yearn"; $result=mysql_db_query($dbname,"SELECT `id`, `year` FROM year WHERE `year_id` = '$model' ORDER BY `year` ")or die(mysql_error()); while(list($id, $name)=mysql_fetch_array($result)){ echo "$name n" ; } } echo "n"; ?>

[/php]

I can paste the ajax script too but I don’t want to get on anyone’s nerves with my very first post here! I think the problem is that the variable for $model hasnt been defined. Any pointers would be much appreciated

Thanks.

I decided to attach the mysql dump just so my question makes a little more sense…

[code]
CREATE TABLE model (
id int(11) NOT NULL auto_increment,
model_id int(11) NOT NULL default ‘0’,
model varchar(255) NOT NULL default ‘’,
PRIMARY KEY (id)
);

INSERT INTO model VALUES (1, 1, ‘escort’);
INSERT INTO model VALUES (2, 1, ‘fiesta’);
INSERT INTO model VALUES (3, 1, ‘mustang’);
INSERT INTO model VALUES (4, 1, ‘eco-sport’);
INSERT INTO model VALUES (5, 2, ‘jetta’);
INSERT INTO model VALUES (6, 2, ‘beatle’);
INSERT INTO model VALUES (7, 2, ‘karmen ghia’);
INSERT INTO model VALUES (8, 2, ‘polo’);
INSERT INTO model VALUES (9, 2, ‘crossfox’);

CREATE TABLE brand (
id int(11) NOT NULL auto_increment,
brand varchar(255) NOT NULL default ‘’,
PRIMARY KEY (id)
);

INSERT INTO brand VALUES (1, ‘Ford’);
INSERT INTO brand VALUES (2, ‘Volkswagen’);

CREATE TABLE year (
id int(11) NOT NULL auto_increment,
year_id varchar(255) NOT NULL default ‘’,
year int(4) NOT NULL default ‘0’,
PRIMARY KEY (id)
);

INSERT INTO year VALUES (1, ‘escort’, 2007);
INSERT INTO year VALUES (2, ‘escort’, 2006);
INSERT INTO year VALUES (3, ‘escort’, 2005);
INSERT INTO year VALUES (4, ‘fiesta’, 2007);[/code][/code]

u have a mysql syntax error:
$result=mysql_db_query($dbname,"SELECT id, year FROM year WHERE ‘year_id’ LIKE $model");
has to be:
$result=mysql_db_query($dbname,“SELECT id, year FROM year WHERE year_id LIKE ‘$model’”);

try if it works now. and please add:
$result=mysql_db_query(…) or die(mysql_error());
so it won’t happen again.

Thanks Q1712, I must have accidently made that simple syntax error when I was posting it but now I fixed it. Still that wasn’t the problem I was mentioning. I am going to post the ajax code too. By the way I was thinking that perhaps someone could make this into a tutorial which could help someone doing something similar in the future?

Anyway here is the code for the ajax… you will see that the variable model isn’t really defined there and thus when the php file trys to access the script it comes up with an empty object value.

Again apologizing for being a noob at this.

[code]

<? echo "n"; echo "Brand : n"; echo "============ n" ; echo "n"; echo "model : n"; echo "=== none === n" ; echo "n"; echo "year : n"; echo "=== none === n" ; echo "n"; ?> [/code]

Heh, well it seems no one has time to help or anybody who does can’t help. So I will post back once I have the solution and a tutorial to go with it.

Thanks.

I think i know where your problem is stemming from.

[code]CREATE TABLE year (
id int(11) NOT NULL auto_increment,
year_id varchar(255) NOT NULL default ‘’,
year int(4) NOT NULL default ‘0’,
PRIMARY KEY (id)
);

INSERT INTO year VALUES (1, ‘escort’, 2007);
INSERT INTO year VALUES (2, ‘escort’, 2006);
INSERT INTO year VALUES (3, ‘escort’, 2005);
INSERT INTO year VALUES (4, ‘fiesta’, 2007);[/code]

In this portion of your code, you are redefining ‘escort’ and ‘fiesta’, you are NOT defining the year. You can see in this code:

INSERT INTO `model` VALUES (1, 1, 'escort'); INSERT INTO `model` VALUES (2, 1, 'fiesta'); INSERT INTO `model` VALUES (3, 1, 'mustang'); INSERT INTO `model` VALUES (4, 1, 'eco-sport'); INSERT INTO `model` VALUES (5, 2, 'jetta'); INSERT INTO `model` VALUES (6, 2, 'beatle'); INSERT INTO `model` VALUES (7, 2, 'karmen ghia'); INSERT INTO `model` VALUES (8, 2, 'polo'); INSERT INTO `model` VALUES (9, 2, 'crossfox');

That the models are being defined to the parent directory, where as your years are not being defined to parent directories. Try:

INSERT INTO `year` VALUES (1, 1, '2007'); INSERT INTO `year` VALUES (2, 1, '2006'); INSERT INTO `year` VALUES (3, 1, '2005'); INSERT INTO `year` VALUES (4, 2, '2007');
And such.

Thanks hcdarkimage! I actually solved it myself just minutes before you posted your solution.

I can’t believe I didn’t see my problem sooner. Anyway, I will post a working version of it real soon with a tutorial as promised. I think drop down boxes with ajax are an extremely common task asked by clients and it could help a lot of beginners like myself to have a tutorial on the subject handy.

Resolved with working example on live site.

http://pilys.com.mx/llantas

Sponsor our Newsletter | Privacy Policy | Terms of Service