Need help with using a checkbox to build a select list

Posted on 2011-04-30
Last Modified: 2012-05-11
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.
Question by:saabStory
    LVL 18

    Accepted Solution

    Here is my idea:

    this will be the form page
    <script type="text/javascript" src=""></script>
    <script type="text/javascript">
    function build_select(first_name,last_name){
       $.post("build_select.php", { 
       		level: ""+$("#level").val()+""
    <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>
    <br />
    <br />
    <div id="show_select_box">

    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 ...
    		?><select name="course"><option>course 1 for level 1</option><option>course 2 for level 1</option></select><?
    		?><select name="course"><option>course 1 for level 2</option><option>course 2 for level 2</option></select><?
    		?><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>

    Open in new window


    Author Closing Comment

    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.

    Author Comment

    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?

    LVL 18

    Expert Comment

    by:Greg Alexander
    So you are saying there can be more than one item checked?

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now