Jump to content

Recommended Posts

I have a table in a database that I need to filter upon using dependent select boxes.  The database was poorly designed and would be difficult to break into separate tables without affecting code written in other places.  I have found some good code and understanding of how to implement the dependent select boxes if you have two tables with one column functioning as a lookup value between the two.

 

I have provided a short bit of sql of what the table looks like with some column names changed.  I would need to first sort by lastname and have firstname dependent on the selection of lastname.  Any help would be greatly appreciated.

 

create table `user` (
`id` int(3) Not Null Auto_increment,
`firstname` varchar (60),
`lastname` varchar (24),
`age` double ,
`hometown` varchar (75),
`job` varchar (75),
`birthdate` date 
); 
insert into `user` (`id`, `firstname`, `lastname`, `age`, `hometown`, `job`, `birthdate`) values
('1','Peter','Griffin','41','Quahog','Brewery','1960-01-01'),
('2','Lois','Griffin','40','Newport','Piano Teacher','1961-08-11'),
('3','Joseph','Swanson','39','Quahog','Police Officer','1962-07-23'),
('4','Glenn','Quagmire','41','Quahog','Pilot','1960-02-28'),
('5','Megan','Griffin','16','Quahog','Student','1984-04-24'),
('6','Stewie','Griffin','2','Quahog','Dictator','2008-03-03');

after lastname is selected, then the first names associated with that name are present in the firstname select box.  i.e. if Griffin is chosen as a last name, then only peter, lois, megan and stewie are selectable in the firstname select box

there in lies my problem.  I understand what technologies are involved with this, I am just unclear how to go about using php and AJAX with one table to filter out the results.  If I could use two tables as shown below, I would not have an issue.

 

Thanks

 

create table `lname` (
   `id` int(3) Not Null Auto_increment,
   `lastname` varchar (24)
);
insert into `lname` (`id`, `lastname`) values
('1', 'Griffin'),
('2', 'Quagmire'),
('3', 'Swanson');

create table `fname` (
   `id` int(3) Not Null,
   `firstname` varchar (60)
);

insert into `fname` (`id`, `firstname`) values
('1','Peter'),
('1','Lois'),
('3','Joseph'),
('2','Glenn'),
('1','Megan'),
('1','Stewie');

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.