JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 7th, 2008, 04:02 AM
jekka jekka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 5 jekka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 31 sec
Reputation Power: 0
Linked combo boxes

Heloo! I'm new to javascript so may be you could help out with this problem
I need two combo boxes, the first one is filled from a database and the second is filled according to what you choose in the first one, also from the database.
If someone has an example, it would be very helpfull

Reply With Quote
  #2  
Old May 7th, 2008, 05:16 AM
PHP-Newb PHP-Newb is offline
Bad Coder
Click here for more information
 
Join Date: Jul 2003
Posts: 1,592 PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 43 m
Reputation Power: 123
How many options are we talking about?
__________________
Under no circumstances click the button, because it is pointless. Thanks!

Reply With Quote
  #3  
Old May 7th, 2008, 06:02 AM
jekka jekka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 5 jekka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 31 sec
Reputation Power: 0
Linked combo boxes

In the first combobox there are about 40 options(which are names of groups in a college ex: CIB0405) the second one selects from the database all the students from that group.

Reply With Quote
  #4  
Old May 7th, 2008, 06:32 AM
PHP-Newb PHP-Newb is offline
Bad Coder
Click here for more information
 
Join Date: Jul 2003
Posts: 1,592 PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 43 m
Reputation Power: 123
Post an example please.

Reply With Quote
  #5  
Old May 7th, 2008, 06:55 AM
jekka jekka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 5 jekka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by jekka
In the first combobox there are about 40 options(which are names of groups in a college ex: CIB0405) the second one selects from the database all the students from that group.
- that's waht i need to do and don't know how
I have one in php, but the comboboxes are not linked. You basically select the group(there are all the possible groups in the combobox) and the student(there are all the students in that combobox). It takes your choices and goes forward if the selected student(combobox 2) is in the selected group(combobox 1).
Here is the sequence of code that selects that creates the comboboxes and fills them with values:

[PHPNET]<tr>
<th> select the group: </th>
<td><?php
$link=mysql_connect("localhost","root","");
$db=mysql_select_db("baza_cfbc");
$qry=mysql_query("SELECT id_grupa, grupa From student_grupa");
echo '<select name="grupa">';
while($rez=mysql_fetch_row($qry))
{
echo '<option value="'.$rez[1].'">'.$rez[1].'</option>';
}
echo '</select>';
?></td>
</tr>
<tr>
<th>select student: </th>
<td><?php
$link=mysql_connect("localhost","root","");
$db=mysql_select_db("baza_cfbc");
echo '<select name="student">';
$query = mysql_query("SELECT id_stud, nume, prenume FROM student_persoane, student_stud WHERE student_stud.id_persoana=student_persoane.id_persoana ");

while ($rez=mysql_fetch_array($query))
echo '<option value="'.$rez['id_stud'].'">'.$rez['prenume'].' '.$rez['nume'].'</option>';

echo '</select>';

?></td></tr>
<tr>[/PHPNET]

Reply With Quote
  #6  
Old May 7th, 2008, 07:22 AM
PHP-Newb PHP-Newb is offline
Bad Coder
Click here for more information
 
Join Date: Jul 2003
Posts: 1,592 PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 43 m
Reputation Power: 123
How are the two tables linked?

Reply With Quote
  #7  
Old May 7th, 2008, 08:55 AM
tom_cos tom_cos is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2007
Posts: 12 tom_cos User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 44 m 16 sec
Reputation Power: 0
Perhaps you can modify the code presented on 2-level combo box
to get what you are looking for.

Reply With Quote
  #8  
Old May 7th, 2008, 11:34 AM
jekka jekka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 5 jekka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by tom_cos
Perhaps you can modify the code presented on ...
to get what you are looking for.


Thanks! thats a good code example, but is there any possibility to work with mysql through javascript?

Reply With Quote
  #9  
Old May 7th, 2008, 11:37 AM
jekka jekka is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2008
Posts: 5 jekka User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 3 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by PHP-Newb
How are the two tables linked?

there is a big database out there. Basically the group ID is contained in the table with students. Student ID is contained in the table with persons(that's where the first and last names are).

Reply With Quote
  #10  
Old May 8th, 2008, 02:45 AM
PHP-Newb PHP-Newb is offline
Bad Coder
Click here for more information
 
Join Date: Jul 2003
Posts: 1,592 PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level)PHP-Newb User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 15 h 43 m
Reputation Power: 123
I was bored yesterday, so I actually wrote this out. It is untested, you have been warned.

Frontend:
Javascript Code:
Original - Javascript Code
  1. <script type="text/javascript">
  2.   var url = "includes/getstudents.php?grupa=";
  3.  
  4.   function init() {
  5.     addEvent(document.getElementById('grupa'), 'change', xmlRange);
  6.   }
  7.  
  8.   function addEvent(obj, type, fn) {
  9.     if(obj.attachEvent) {
  10.       obj['e'+type+fn] = fn;
  11.       obj[type+fn] = function(){obj['e'+type+fn](window.event);}
  12.       obj.attachEvent('on'+type, obj[type+fn]);
  13.     }
  14.     else obj.addEventListener(type, fn, false);
  15.   }
  16.  
  17.   function insertReturn() {
  18.     if(http.readyState == 4) {
  19.       results = http.responseText.split(",");
  20.  
  21.       var Students = document.getElementById('students');
  22.       while(Students.length != 0) Students.options[Students.length - 1] = null;
  23.  
  24.       for(var i=0; i<results.length; i++) {
  25.         var currentStudent = results[i].split("|");
  26.         Students.options[i] = new Option(currentStudent[0],currentStudent[1]);
  27.       }
  28.       document.getElementById('students').disabled = false;
  29.     }
  30.   }
  31.  
  32.   function xmlRange() {
  33.     document.getElementById('students').disabled = true;
  34.     var Grupa = document.getElementById('grupa');
  35.  
  36.     http.open("GET", url + escape(Grupa.options[Grupa.selectedIndex].value), true);
  37.     http.onreadystatechange = insertReturn;
  38.     http.send(null);
  39.   }
  40.  
  41.   function getHTTPObject() {
  42.     var xmlhttp;
  43.     /*@cc_on
  44.     @if (@_jscript_version >= 5)
  45.       try xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  46.       catch (e) {
  47.         try xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  48.         catch (E) xmlhttp = false;
  49.       }
  50.     @else
  51.     xmlhttp = false;
  52.     @end @*/
  53.     if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {
  54.       try xmlhttp = new XMLHttpRequest();
  55.       catch (e) xmlhttp = false;
  56.     }
  57.     return xmlhttp;
  58.   }
  59.   var http = getHTTPObject();
  60.   window.onload = init;
  61. </script>
  62.  


PHP Code:
Original - PHP Code
  1. <?PHP
  2.   $link = mysql_connect("localhost","root","");
  3.   $db   = mysql_select_db("baza_cfbc");
  4.   $qry  = mysql_query("SELECT id_grupa, grupa From student_grupa");
  5.  
  6.   echo '  <select id="grupa" name="grupa">';
  7.   while($rez = mysql_fetch_row($qry)) {
  8.     echo '    <option value="'.$rez[0].'">'.$rez[1].'</option>';
  9.   }
  10.   echo '  </select>';
  11. ?>
  12.  
  13.   <select id="student" name="student">
  14.     <option>- Select a Group -</option>
  15.   </select>
  16.  


Backend(getstudents.php):
PHP Code:
Original - PHP Code
  1. <?PHP
  2.   $GrupaID = mysql_real_escape_string($_GET['grupa']);
  3.   $link = mysql_connect("localhost","root","");
  4.   $db = mysql_select_db("baza_cfbc");
  5.   $query = mysql_query("SELECT id_stud, nume, prenume FROM student_persoane, student_stud WHERE id_stud = '{$GrupaID}'");
  6.   $studentsReturn = '';
  7.  
  8.   while($rez = mysql_fetch_array($query)) {
  9.     $studentsReturn .= $rez['id_stud'] .'|'. $rez['prenume'] .' '.