Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dependent dynamic select form from a MySQL database

Posted on 2006-07-15
3
Medium Priority
?
236 Views
Last Modified: 2013-12-12
Ok, I am not a Javascript coder nor do I use ASP but those are most of the examples for building a dependent dynamic select menu form I've found in google (yes, I've tried searching for a solution but few PHP/MySQL/Javascript comes up).

Well, I've been altering something similar to this:
[php]
 //************** start dynamic drop-down list magic ***************//
  print "<script language=\"javascript\"><!--\n";
  $classes = mysql_query("SELECT class FROM classes ORDER BY class");
  print "var labIDs = new Array(" . mysql_num_rows($classes) . ");\n";
  print "var labNames = new Array(" . mysql_num_rows($classes) . ");\n";
  while (list($class) = mysql_fetch_array($classes)) {
     $labs = mysql_query("SELECT DISTINCT labID FROM duedates WHERE class = $class ORDER BY due");
     print "labIDs[$class] = new Array(" . (sizeof($labs) + 1) . ");\n";
     print "labNames[$class] = new Array(" . (sizeof($labs) + 1) . ");\n";
     $i = 1;
     print "labIDs[$class][0] = 0;\n";
     print "labNames[$class][0] = 0;\n";
     while (list($lab) = mysql_fetch_array($labs)) {
        print "labIDs[$class][$i] = '$lab';\n";
        print "labNames[$class][" . $i++ . "] = '";
        print addslashes(mysql_result(mysql_query("SELECT name FROM labs WHERE class = $class AND labID = $lab"), 1));
        print "';\n";
     }
  }
  print <<<EOJS
function clean(what) {
  for (; what.options.length > 1;) {
     what.options[what.options.length - 1] = null;
  }
}

function updateLabs(which) {
  with (document.forms[0]) {
     clean(which);
     curClass = course.options[course.selectedIndex].value;
     for (var i = 1; i < labIDs[curClass].length; i++) {
       which.options[which.options.length] = new Option(labIDs[curClass][i] + ". " + labNames[curClass][i], labIDs[curClass][i]);
     }
  }
}
//--></script>
EOJS;  //************** end dynamic drop-down list magic ***************//

  print form_open();
  //replace the old static drop-down tag with a new dynamic one
  print "Course: <select name=\"course\" onChange=\"javascript: updateLabs(lab);\">\n";
  $classes = mysql_query("SELECT class FROM classes ORDER BY class")
  print "<option value=0 selected>Choose one:</option>\n";
  while (list($class) = mysql_fetch_assoc($classes)) {
    print "<option value=$class>$class</option>\n";
  }
  print "</select><br>\n";
  print "Lab: <select name=\"lab\">\n<option value=0>Choose one\n</select><br>\n"; //will get filled automagically [/php]

which I found here. http://forums.devshed.com/showpost.php?p=608686&postcount=41

However, I just can't get the Javascript to work.

So, would anyone tweak that script for it to work with my database or show me another solution?

This is my database structure, where category.categoryid is a primary key and subjectcat.categoryid is a foreign key.
//Table: category
[categoryid] [category]
1        math
2       computer
3       science
4       social science
5       history
6       humanities
7       foreign language
8       english
9       education
10       engineering
11       business

//Table: subjectcat
[subjectid] [subject] [categoryid]
100        arithmetic        1
101       general math       1
102       pre-algebra       1
103       algebra beginning elem       1
200        microsoft windows        2  
201       microsoft office       2
202       troubleshooting       2
300        general science        3

and so on....

I want one select menu form to list the categories and then another select menu form to dynamically generate the 'subjects' based on the other select form 'category' selected.

Once the user click submit, the $subjectid value will be passed as a $_GET['subjectid']...

Example to the url:
http://www.domain.com/index.php?subjectid=3

Something like that. Can anyone help?
0
Comment
Question by:identityless
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Accepted Solution

by:
cristi_2112 earned 1000 total points
ID: 17121812
Hello,

You should post a copy of the source generated by this page.
Anyway, what I can so far:
- instead of - print "<option value=$class>$class</option>\n";
  use -  print "<option value=\"$class\">$class</option>\n";

- close the <form> tag

But again, you should view source of the page and copy/paste it here, because the script should work.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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