Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 270
  • Last Modified:

Need help with using a checkbox to build a select list

I've got a requirement in a form that I don't know how to build. At the top of the form, I've got a list of grade levels.  Each grade level corresponds to a variable number of programs that grade level is authorized to take.

For instance, if someone selected 'Freshman' in a checkbox at the top of the form, at the bottom of the form, I want to create a select list of programs/courses the grade level is authorized to take. Select 2 grade levels and the list below includes programs/courses for both grade levels and so on.  Select 'All' and you get everything.

So, for the sake of an example, say the program level choices are 'All', 'Freshman' and 'Junior' - these are not the real names of the choices nor the actual number - it's only for this example.  Also, for the sake of example, say the courses are:

Freshman: Baskets 101, Basic Typing, Beginning Cooking
Junior: Intermediate Camping, Beginning Autocross, Intermediate Knot-Tying

Just FYI, the real list of courses will come from a database of over 200.

Hopefully this is clear enough - many thanks in advance for any and all help.
0
saabStory
Asked:
saabStory
  • 3
  • 2
1 Solution
 
Greg AlexanderLead DeveloperCommented:
Here is my idea:

this will be the form page
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script>

<script type="text/javascript">
function build_select(first_name,last_name){
   $.post("build_select.php", { 
   		level: ""+$("#level").val()+""
   },
   function(data){
   		$("#show_select_box").html(data);
   }); 
}
</script>

<div>
<select name="level" id="level" onchange="build_select()">
	<option value="">Choose a Level</option>
	<option value="1">level 1</option>
    <option value="2">level 2</option>
    <option value="all">All Courses</option>
</select>
</div>

<br />
<br />
<div id="show_select_box">

</div>

Open in new window


This will be the build_select.php page that will output your select box respectively
<?
$level = $_POST['level'];
//just an example, but basically you will build your select box on this page and ajax the result in


//for example sake I will just build a switch statement, but you will build it from a query
//i.e. select * from courses where level = $level ...

switch($level){
	case'1':
		?><select name="course"><option>course 1 for level 1</option><option>course 2 for level 1</option></select><?
	break;	
	case'2':
		?><select name="course"><option>course 1 for level 2</option><option>course 2 for level 2</option></select><?
	break;	
	case'all':
		?><select name="course">
        	<option>course 1 for level 1</option>
            <option>course 2 for level 1</option>
            <option>course 1 for level 2</option>
            <option>course 2 for level 2</option>
            <option>course 1 for level 3</option>
            <option>course 2 for level 3</option>
		</select>
		<?
	break;	
}
?>

Open in new window


0
 
saabStoryAuthor Commented:
That looks pretty slick.  I had considered modifying an ajax-enabled  dependent select list I use from time to time but hadn't got around to working with it.  I really like the use of the case here - simplifies things a great deal.
0
 
saabStoryAuthor Commented:
One thing just occurred to me.  You're using two select lists.  In my instance, I start with a checkbox and populate the select list based on which checkbox has been checked.  How would I modify this to an onclick?

0
 
Greg AlexanderLead DeveloperCommented:
So you are saying there can be more than one item checked?
0
 
saabStoryAuthor Commented:
That's what the client would like.  I'll post another question this evening for this wrinkle - don't want to pick your brain for free - :-)

I'll post a link to it once it's up.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now