Link to home
Start Free TrialLog in
Avatar of zamirjalil
zamirjalil

asked on

Urgent! Javascripts Onchage event question.

Hello experts,

I not expert in Javascripts, but I want to do this in Javascript and still not success.
I get this free Javascript from internet and modified some to meet my requirements which all the data in the select boxes are retrived from database. Then come the taugh part which not provided in the script. I want the script to automatically check the available number (also from database) after the second selection (select 2).

For example: I select first select box and it automatically do onchange script to effect select box 2. Then I select second select box and I want it to automatically checked the checkboxes(disabled) that in used. The rest checkbox is remain uncheck.

Let Say:

Select 1 :
subselect 1
subselect 2

Select 2:
subsubselect 1
subsubselect 2
subsubselect 3

I select subselect1 -> subsubselect2 -> (available number are 1,4,14,28). These 4 checkboxes should automatically checked with disabled and the rest remain uncheck.

-------------------------------------

below is my code:

<HTML><HEAD>
<TITLE>WebReference.com</title>
<META name=description content=Cascading Menu generated at SuperTom.Com>

<!-- original code and docs at http://www.webreference.com/dev/menus/ -->
<!-- you need this stuff below -->
<META content=history name=save>
<STYLE>.saveHistory {
      BEHAVIOR: url(#default#savehistory)
}
</style>
<!-- you need this stuff above-->

<SCRIPT language=JavaScript>
<!--
v=false;
//-->
</script>

<SCRIPT language=JavaScript1.1>
<!--
if (typeof(Option)+"" != "undefined") v=true;
//-->
</script>

<SCRIPT language=JavaScript>
<!--
/*
// GENERATED BY MENUGEN located at http://www.supertom.com/menugen/
// Menugen Copyright 2000 Tom Melendez
// Universal Related Select Menus - cascading popdown menus
// by Andrew King. v1.34 19990720
// Copyright (c) 1999 internet.com LLC. All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Originally published and documented at http://www.webreference.com
// Contact aking@internet.com for all other uses.
//
// Change History
// 3-3-96 code optimized, modularized, and made universal-abk
// 3-8-97 jumpPage1 and 2 consolidated into generalized func
// 3-10-97 changeMenu added jumpPage(0,0), for non-ns3 browsers
// make popup act like a live popup when no related functionality
// 8-5-97 browser detect script changed to check for option functionality
// 9-11-97 aln changed to options.length, as IE 4 does not support
// nulling out options
// 12-22-98 added SIZE att to make select lists, used named forms/elements
// 1-1-99 added getFormNum to fix netscape 2 bug (doesn't support named fms)
//  pass formName, elementNum to relate and jmp as workaround
//  pass this.form,0 and increment instead of decrement in relate to get
//   next form
//  assumes menu2 follows menu1 in HTML
// tested in mac ie3.01/ns2.02 (live), ns 3.01,4.05,4.5 and ie4.01 related
// 990204 - adopted a more modular/oo approach using two D arrays, thanks
//   to Jacob Berner <leo7278@hotmail.com> for his input
// 990701 - fixed back button bug in ie4+ by resetting all forms onload-abk
//    thanks to peter belesis (pbel@internet.com) for this fix
// 990714 - added IE5 persistence - abk pb
// 990715 - went open source, created stripped down example files - abk
// 990720 - jmp() fix getFormNum(formName);->
//     var formNum = getFormNum(formName);
//  if (v) {..} added around newCat array creation for old browsers
// 990721 - getFormNum/relate tweaks, unnec code removed,
//     ns2.0 mac fix - getFormNum returns -1 in nested tables
//     new jmp bypasses prob - thx to thanks to Michael Guitton
//     saramaca@mail.dotcom.fr
// 990722 - var for scope added, jmp chgd to named form for ns2.02 bug-abk
*/
if(v){a=new Array(22);}

function getFormNum (formName) {
      var formNum =-1;
      for (i=0;i<document.forms.length;i++){
            tempForm = document.forms[i];
            if (formName == tempForm) {
                  formNum = i;
                  break;
            }
      }
      return formNum;
}

var catsIndex = -1;
var itemsIndex;

if (v) { // ns 2 fix
function newCat(){
      catsIndex++;
      a[catsIndex] = new Array();
      itemsIndex = 0;
}

function O(txt,url) {
      a[catsIndex][itemsIndex]=new myOptions(txt,url);
      itemsIndex++;
}

function myOptions(text,value){
      this.text = text;
      this.value = value;
}

// fill array here
<?
@$dbhost="localhost";
@$dbuser="mydbuser";
@$dbpass="mydbpasswd";
@$dbname="mydbname";


mysql_connect("$dbhost", "$dbuser", "$dbpass") OR die("Couldn't connect to MySQL");
mysql_select_db("$dbname") OR die("Couldn't open db. Error if any was: ".mysql_error() );



$q = "SELECT DISTINCT fld1 from tbl1";
$r = mysql_query($q);

while ($rows=mysql_fetch_array($r)) {
      $t=$rows["fld1"];
      echo "newCat();";
      $q1="SELECT DISTINCT fld2 from tbl1 where fld1='$t'";
      
      $r1 = mysql_query($q1);
      while ($rows1=mysql_fetch_array($r1)) {
            $w = $rows1["fld2"];
            echo 'O("'.$w.'","'.$w.'");';
      }
}
?>

} // if (v)



function relate(formName,elementNum,j) {
    if(v){
        var formNum = getFormNum(formName);
         if (formNum>=0) {
              formNum++; // reference next form, assume it follows in HTML
              with (document.forms[formNum].elements[elementNum]) {
                    for(i=options.length-1;i>0;i--) options[i] = null; // null out in reverse order (bug workarnd)
                    for(i=0;i<a[j].length;i++){
                          options[i] = new Option(a[j][i].text,a[j][i].value);
                    }
                    options[0].selected = true;
              }
        }
    }
    else {
        //jmp(formName,elementNum);
    }
}

// BACK BUTTON FIX for ie4+- or
// MEMORY-CACHE-STORING-ONLY-INDEX-AND-NOT-CONTENT
//
// from peter belesis:
// IE4+ remembers the index of each SELECT but NOT the CONTENTS of each
// SELECT, so it gets it wrong.
//
// it has to do with MEMORY CACHE (where form input is stored) and how
// IE stores information about SELECT menus.
//
// IE stores the selectedINDEX ONLY of the SELECT menu, not the
// CONTENTS-AT-THE-TIME-OF-SELECTION
//
// when we return to a page, it displays the default contents of each
// SELECT, grabs the stored index from cache and aligns the default
// contents to that index.
//
// Netscape, on the other hand, seems to remember both INDEX and CONTENTS
// added ie5 persistence 990714

function IEsetup(){
      if(!document.all) return;
      IE5 = navigator.appVersion.indexOf("5.")!=-1;
      if(!IE5) {
            for (i=0;i<document.forms.length;i++) {
                  document.forms[i].reset();
            }
      }
}

window.onload = IEsetup;

//-->
</script>

</head>
<BODY bgColor=#ffffff>
<CENTER>

<TABLE bgColor=#FFFFFF border=1 cellPadding=0 cellSpacing=0 bordercolor=#000000>
<TR><TD>

<TABLE bgColor=#FFFFFF border=0 cellPadding=4 cellSpacing=2>
  <TBODY>
  <TR>
    <TD vAlign=top>select 1</td>
      <TD vAlign=top>
      <FORM action= method=post name=f1 onsubmit="return false;">
      <SELECT class=saveHistory id=m1 name=m1 onchange=relate(this.form,0,this.selectedIndex)>
      <?
      $sql = "SELECT DISTINCT fld1 from tbl1";
      $rsql = mysql_query($sql);

      while ($rw = mysql_fetch_array($rsql)) {
            $w = $rw["fld1"];
      ?>
            <option value="<?=$rw["fld1"]?>"><?echo $rw["fld1"];?></option>
      <? } ?>
      </select>
      </form></td>
      </tr>
    <TR>
    <TD vAlign=top>select 2</td>
      <TD vAlign=top>
      <FORM action= method=post name=f2 onsubmit="return false;">
      <SELECT class=saveHistory id=m2 name=m2>
      <?
      $sql = "SELECT DISTINCT fld2 from tbl1";
      $rsql = mysql_query($sql);

      while ($rw = mysql_fetch_array($rsql)) {
            $w = $rw["fld2"];
      ?>
            <option value="<?=$rw["fld2"]?>"><?echo $rw["fld2"];?></option>
      <? } ?>
      </select>

</form></td></tr>
</tbody></table>
</td></tr></table>
  <table width="446" border="0">
    <tr>
      <td>
        <input type="checkbox" name="checkbox" value="checkbox">
        1 </td>
      <td>
        <input type="checkbox" name="checkbox2" value="checkbox">
        2 </td>
      <td>
        <input type="checkbox" name="checkbox3" value="checkbox">
        3 </td>
      <td>
        <input type="checkbox" name="checkbox4" value="checkbox">
        4 </td>
      <td>
        <input type="checkbox" name="checkbox5" value="checkbox">
        5 </td>
      <td>
        <input type="checkbox" name="checkbox6" value="checkbox">
        6 </td>
      <td>
        <input type="checkbox" name="checkbox7" value="checkbox">
        7 </td>
      <td>
        <input type="checkbox" name="checkbox8" value="checkbox">
        8 </td>
      <td>
        <input type="checkbox" name="checkbox9" value="checkbox">
        9 </td>
      <td>
        <input type="checkbox" name="checkbox10" value="checkbox">
        10 </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="checkbox11" value="checkbox">
        11 </td>
      <td>
        <input type="checkbox" name="checkbox12" value="checkbox">
        12 </td>
      <td>
        <input type="checkbox" name="checkbox13" value="checkbox">
        13 </td>
      <td>
        <input type="checkbox" name="checkbox14" value="checkbox">
        14 </td>
      <td>
        <input type="checkbox" name="checkbox15" value="checkbox">
        15 </td>
      <td>
        <input type="checkbox" name="checkbox16" value="checkbox">
        16 </td>
      <td>
        <input type="checkbox" name="checkbox17" value="checkbox">
        17 </td>
      <td>
        <input type="checkbox" name="checkbox18" value="checkbox">
        18 </td>
      <td>
        <input type="checkbox" name="checkbox19" value="checkbox">
        19 </td>
      <td>
        <input type="checkbox" name="checkbox20" value="checkbox">
        20 </td>
    </tr>
    <tr>
      <td>
        <input type="checkbox" name="checkbox21" value="checkbox">
        21 </td>
      <td>
        <input type="checkbox" name="checkbox22" value="checkbox">
        22 </td>
      <td>
        <input type="checkbox" name="checkbox23" value="checkbox">
        23 </td>
      <td>
        <input type="checkbox" name="checkbox24" value="checkbox">
        24 </td>
      <td>
        <input type="checkbox" name="checkbox25" value="checkbox">
        25 </td>
      <td>
        <input type="checkbox" name="checkbox26" value="checkbox">
        26 </td>
      <td>
        <input type="checkbox" name="checkbox27" value="checkbox">
        27 </td>
      <td>
        <input type="checkbox" name="checkbox28" value="checkbox">
        28</td>
      <td>
        <input type="checkbox" name="checkbox29" value="checkbox">
        29 </td>
      <td>
        <input type="checkbox" name="checkbox30" value="checkbox">
        30 </td>
    </tr>
  </table>
  <p>&nbsp;</p>
</center></body></html>





Experts out there, please help me.

zamir
Avatar of jaysolomon
jaysolomon

Something like this

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
<!--Begin
function getSelectValue(selObj){
      return selObj.options[selObj.selectedIndex].value;
}
function checkDisabledBoxes(fObj){
      // 1 4 14 28
      if(getSelectValue(fObj.sel2) == "subsubselect2"){
            fObj.checkbox1.checked = true;
            //fObj.checkbox1.disabled = true;
            fObj.checkbox4.checked = true;
            //fObj.checkbox4.disabled = true;
            fObj.checkbox14.checked = true;
            //fObj.checkbox14.disabled = true;
            fObj.checkbox28.checked = true;
            //fObj.checkbox28.disabled = true;
      }
}
//End-->
</script>
</head>
<body>
<form name="form1" method="post" action="">
<select name="sel2" onchange="checkDisabledBoxes(this.form);">
<option>select</option>
<option value="subsubselect2">subsubselect2</option>
</select>
<input type="checkbox" name="checkbox1" disabled value="y">
<input type="checkbox" name="checkbox4" disabled value="y">
<input type="checkbox" name="checkbox14" disabled value="y">
<input type="checkbox" name="checkbox28" disabled value="y">

</form>
</body>
</html>
You do know if its checked and disabled, you will not get the checked value in the final results?
Avatar of zamirjalil

ASKER

thanks jay, actually the final result will be checked and disabled + enable checkboxes. What javascript will do is to validate any availabe number (checkboxes) from the sql query. Disabled means not available. But it is still display all the checkboxes.

What you did is correct but I want the checkbox is enable wht the page is load. Only after the second selection, then certain checkbox will disabled. this will be checked thru sql query.

ASKER CERTIFIED SOLUTION
Avatar of jaysolomon
jaysolomon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial