Fill a list box based on drop down

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dougp25
Forum Newbie
Posts: 10
Joined: Tue Mar 30, 2004 12:29 pm

Fill a list box based on drop down

Post by dougp25 »

I have a form attached to a database of students, Grades 5-8.

I have a dropdown list that has the grades teachers can choose from. When they choose, for example, 7, I would like thelist box below to fill with the 7th grade students. Coming from VBA (ugh) there was an "after update" event that would run when you changed the dropdown. I don't mind if they have to click a button that says "GO" after they pick a grade, but how do I fill that list box?

Thanks.
amithn12
Forum Newbie
Posts: 5
Joined: Thu Nov 06, 2003 5:40 am

Post by amithn12 »

Once u select a grade from the drop down box, say 7 u can do either of 2 things , one u can reload the same page but u r passing parameters to the page when reloading, which u can get by POST method and query u r database based on the grade and and get the students belonging to that grade and fill it in the List box.

or

ucan give the GO button and do the same thing.


For Example:

when u first come to the page :
have a flag and a grade variable.
$flag,$grade

$flag = $_POST['flag'];
$grade = $_POST['grade'];

Check if the flag is 0 which is the initial value u will be passing from the last page.then do not fill the list box with Student Names.
Once if they select a Grade and click GO u make a hidden field and give the value as one for flag
and when u submit the form u will have the value of flag as 1 then do the required database qeries from the
selected drop down list. the value of the dropdown list will be posted so give appropriate values so that u can
search the databse with the values and fill the list box that is it.


for ex:

if ($flag == 0)
{
do not fill list box
}


if ($flag ==1)
{
search the databse with the Grade value and put it int he list box
}
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Or you can do it with javascript. I'm not entirely sure how, I'm not a javascript expert but it would be a function. In the textbox you'll have

onChange="update()"

and update will be

if(form.textbox.value==7)
form.listbox.value = "list \n of \n Student \n names

where the names are culled from your dbase. When you build the page uyou can populate the javascript from the php. I'm not sure exactly how the javasript goes, you'll have to look into it. But thats how I would do it.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Ya, in order to be as responsive to the user as possible, I'd suggest doing it how ~magicrobotmonkey suggests.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply