Click here to Skip to main content
15,940,173 members
Please Sign up or sign in to vote.
5.00/5 (1 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.

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.
Share this answer
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
Share this answer

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900