Click here to Skip to main content
13,048,788 members (70,035 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I want to create 2 select boxes. The option of the first box chosen will decide the options visible in the second select box. For example:

Select box A contains options 2010, 2011, 2012

Select box B contains movies names according their release year.

Now when say option 2012 is selected in box A, only those movies which have their release year on 2012 are displayed.

Please help coding in PHP/MySQL.

Thanks in advance.
Posted 17-Sep-12 5:37am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You need a simple query with a WHERE clause!

  movieYear = '2012'

You will have to pass movieYear as a parameter to query where its value will be equal to boxA option selected. Set query result as a datasource of this boxB.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Hi Friend,
These tips may help you. I am sorry that I will not be providing you with codes since I have not one ready, and it really takes time to create one. But I will be giving you hints toward which you can move your way to reach the success.

1. First of all create a MySQL database and possibly create two tables, the one for the year and the another for film name or something like that, you are able to create one that seems more logical to you.

2. The second step is using AJAX (Yes, Javascripting) unless you have planned to reload the page everytime the user select an Year from the first select box.

3. The third step will be if you are going with creating an AJAX script. Create a function that will submit the Value of first selectbox on a onchange() event.

4. Create a page in php without any html inside it. This page will receive this data (the value of Selectbox1) and process it using MYSQL query to get a list of the array that will be storing the name of the Movies. Echo this data using this kind of code as example:
foreach($dataarray as $moviename)
 echo '<option>'.$moviename.'</option>';

5. In the ajax script, simply put this code in a block defined with ID name like this:
if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
 document.getElementById("movieid").innerHTML = xmlhttp.responseText;

and, in the html part define something like this:
<div id="movieid"><option>Wait...</option></div>

6. Now you have your very own professional looking Select Box System, in which the Items displayed in one box will depend on the item selected in another box.

Hope it has helped you. I will possibly post the code if I get time to write one very soon.

Tushar Srivastava

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Sep 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100