Link to home
Start Free TrialLog in
Avatar of guiaragaoreis
guiaragaoreis

asked on

ComboBox ASP + Javascript

I don't understand why don't work.

Can anyone help me?

Thanks,
Gui Reis


<!-- #include virtual="/conteudos/incSystem.asp"-->
<%
set conn=server.createobject("ADODB.Connection")
conn.open connection
%>

<html>
<head>
<title>Populate Select Options</title>
</head>

<body>



<script language=Javascript>
<%
   Dim count
   Dim DistID
   Dim DistTXT
   count = 0
   SQL = "SELECT dist_code, dist_descricao FROM bvu_distrito"
   set rs = Conn.Execute(SQL)
   Do Until rs.eof
      if count = 0 then
         DistID = rs("dist_code")
         DistTXT = chr(34) & rs("dist_descricao") & chr(34)
         
      else
         DistID = rs("dist_code") & "," & DistID
         DistTXT = chr(34) & rs("dist_descricao") & chr(34) & "," & DistTXT
      end if
      count = count + 1
      rs.movenext
   Loop
   DistID = DistID & ",-1"
   DistTXT = DistTXT & ",-1"
   rs.close
   set rs = nothing

%>
// now populate the Javascript variables
var ArrayDistID = new Array(<%=DistID%>);
var ArrayDistTXT = new Array(<%=DistTXT%>);
// now show them so you know this works!
alert(ArrayDistID + '\n' + ArrayDistTXT);
</script>

<script language="javascript">
function ManageStates(value)
{
      var disp = "block";
      if (value.match ("USA") == null){
            disp = "none";
      }
      var states = document.MyForm.Conuntry_States;
      if (states != null) {
            states.style.display = disp;
      }
}
function ManageList (value) {
      var USAStateList = new Array('FL', 'NY', 'CA');
      var CANStateList = new Array('Can_State1', 'Can_state2');
      var UKRStateList = null;

      var stateList    = null;
      //
      // Clean the current list
      //
      for (var i=0; i < document.MyForm.Conuntry_States.length; i++)
      {
                  document.MyForm.Conuntry_States.options[i]=null;
      }

      document.MyForm.Conuntry_States.length = 0;
      //
      // Evaluate and assign the list
      //
      if (value.match ("USA") != null) {
            stateList = USAStateList;
      }
      if (value.match ("CAN") !=  null) {
            stateList = CANStateList;
      }
      if (value.match ("UKR") != null) {
            stateList = UKRStateList;
      }

      //
      // Update the Option Value, Text, Length and Display
      //
      if (stateList != null) {
            for (var j=0; j < stateList.length; j++)
            {
                  //alert (" State " + stateList[j]);
                  eval ("var optionText"+j+" = new Option(stateList[j])")
                  eval ("var optionValue"+j+" = stateList[j]")
                  eval ("document.MyForm.Conuntry_States.options[j] = optionText" + j)
                  eval ("document.MyForm.Conuntry_States.options[j].value = optionValue" + j)
            }
            document.MyForm.Conuntry_States.length = stateList.length;
            document.MyForm.Conuntry_States.style.display = "block";
      }
      else {
            document.MyForm.Conuntry_States.style.display = "none";
      }
}
</script>










<form name="MyForm">
      <table>
      <tr>
            <th align="left">
      <select name="Country"  onChange="ManageList(document.MyForm.Country.value);">
            <option value="CAN">Canada</option>
            <option value="UKR">Ukraine</option>
            <option value="USA" selected>United States of America</option>
      </select>
            </th>
      </tr>
      <tr>
            <th align="left">
      <select name="Conuntry_States">
            <option value="FL">Florida</option>
            <option value="CA">California</option>
            <option value="NY">New York</option>
      </select>
            </th>
      </tr>
      </table>
</form>
</body>
</html>
Avatar of CWS (haripriya)
CWS (haripriya)
Flag of India image

Can you explain what problem you are facing with this script?
Avatar of guiaragaoreis
guiaragaoreis

ASKER

I cannot insert the javascript array in slave combobox. I have always an error in page. i can buil javascript arrays with sql data but i can't make a combobox relations with the 2 tables.
The script its wrong

Sorry

REAL SCRIPT

++++++++++++++++++++++++++++++++++++++

<!-- #include virtual="/conteudos/incSystem.asp"-->
<%
set conn=server.createobject("ADODB.Connection")
conn.open connection

function writeArrays1()
            strARR1 = ""
            strARR2 = ""
            response.write "<SCRIPT LANGUAGE='JavaScript'>" & vbCrLf
            response.write "<!-- Begin" & vbCrLf
            sqlstmt1 = "SELECT * from bvu_distrito  ORDER BY dist_code"
                  set rs=conn.execute(sqlstmt1)
            if not rs.eof then
                  do while not rs.eof
                        x = 1
                        t1VarName = rs("dist_descricao")
                        t1VarNum  = rs("dist_code")            
                        t1VarAbbr = rs("dist_code")
                        strARR1   = strARR1 & "var " & t1VarAbbr & "Array =  new Array(""('---------','',true,true)"","
                                               
                        sqlstmt2 = "SELECT * from bvu_concelho WHERE dist_code = " & t1VarNum & " ORDER BY conc_descricao ASC; "
                                    set rs2=conn.execute(sqlstmt2)
                        if not rs2.eof then            
                              do while not rs2.eof
                                    y = 1
                                    t2VarName = rs2("conc_descricao")                                    
                                    t2VarNum  = rs2("conc_code")
                                    if x > 1 then strARR1 = strARR1 & ""","
                                    strARR1 = strARR1 & """('" & t2VarName & "','" & t2VarNum & "')"                        
                                    x = x + 1
                              rs2.MoveNext
                              loop
                        end if
                        rs2.close()
                        strARR1 = strARR1 &  """);" & vbCrLf
                  rs.MoveNext
                  loop
            end if
            rs.close()                        
            response.write strARR1 & vbCrLf            
            response.write "//End -->" & vbCrLf
            response.write "</script>" & vbCrLf
      end function
%>
<html>
<head>
<meta http-equiv="Content-Language" content="pt">
<title><%=title%></title>
<link href="geral.css" rel="stylesheet" type="text/css">
</head>
<SCRIPT LANGUAGE="JavaScript">
function jsCatT1(inForm,selected) {
var selectedArray = eval(selected + "Array");
while (selectedArray.length < inForm.Branch.options.length) {
inForm.Branch.options[(inForm.Branch.options.length - 1)] = null;
}
for (var i=0; i < selectedArray.length; i++) {
eval("inForm.Branch.options[i]=" + "new Option" + selectedArray[i]);
}
if (inForm.Division.options[0].value == '') {
inForm.Division.options[0]= null;
if ( navigator.appName == 'Netscape') {
if (parseInt(navigator.appVersion) < 4) {
window.history.go(0);
}
else {        
if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
window.history.go(0);
            }
         }
      }
   }
}
</SCRIPT>
<%=writeArrays1%>

<form name="form" action="">

<select name="Division" onChange="jsCatT1(document.form,document.form.Division.options[document.form.Division.selectedIndex].value)"

<%
      sqlstmt = "SELECT * from bvu_distrito ORDER BY dist_descricao ASC; "
      set rs=Conn.execute(sqlstmt)
      if not rs.eof then
        do while not rs.eof
         optionValue = rs("dist_code")
          optionLabel = rs("dist_descricao")                                                                                                      
           response.write "<option value='" & optionValue & "'"
          'if cstr(optionValue) = cstr(xxx) then response.write " selected"
              response.write ">" & optionLabel & "</option>" & vbCrLf
          rs.MoveNext
        loop
      end if
      rs.close      
%>
                     
</select>

<select name="Branch">
</select>

</form>
Avatar of hielo
In javascript, variables names are NOT allowed to begin with a number. This:
strARR1   = strARR1 & "var " & t1VarAbbr & "Array =  new Array(""('---------','',true,true)"","

is generating
var 01Array =  ...

add an underscore infront of the number:
var _01Array =  ...

and change:
var selectedArray = eval( selected + "Array");

to:
var selectedArray = eval("_"+selected + "Array");

Also, change:

<!-- #include virtual="/conteudos/incSystem.asp"-->
<%
set conn=server.createobject("ADODB.Connection")
conn.open connection 
function writeArrays1()
            strARR1 = ""
            strARR2 = ""
            response.write "<SCRIPT LANGUAGE='JavaScript'>" & vbCrLf
            response.write "<!-- " & vbCrLf & "//Begin" & vbCrLf
            sqlstmt1 = "SELECT * from bvu_distrito  ORDER BY dist_code" 
                  set rs=conn.execute(sqlstmt1)
            if not rs.eof then
                  do while not rs.eof
                        x = 1
                        t1VarName = rs("dist_descricao")
                        t1VarNum  = rs("dist_code")            
                        t1VarAbbr = rs("dist_code")
                        strARR1   = strARR1 & "var _" & t1VarAbbr & "Array =  new Array(""('---------','',true,true)"","
                                                
                        sqlstmt2 = "SELECT * from bvu_concelho WHERE dist_code = " & t1VarNum & " ORDER BY conc_descricao ASC; "
                                    set rs2=conn.execute(sqlstmt2)
                        if not rs2.eof then            
                              do while not rs2.eof
                                    y = 1
                                    t2VarName = rs2("conc_descricao")                                    
                                    t2VarNum  = rs2("conc_code")
                                    if x > 1 then strARR1 = strARR1 & ""","
                                    strARR1 = strARR1 & """('" & t2VarName & "','" & t2VarNum & "')"                        
                                    x = x + 1
                              rs2.MoveNext
                              loop
                        end if
                        rs2.close()
                        strARR1 = strARR1 &  """);" & vbCrLf
                  rs.MoveNext
                  loop
            end if
            rs.close()                        
            response.write strARR1 & vbCrLf            
            response.write "//End -->" & vbCrLf
            response.write "</script>" & vbCrLf
      end function
%>
<html>
<head>
<meta http-equiv="Content-Language" content="pt">
<title><%=title%></title>
<link href="geral.css" rel="stylesheet" type="text/css">
</head>
<SCRIPT LANGUAGE="JavaScript">
function jsCatT1(inForm,selected) {
var selectedArray = eval("_"+selected + "Array");
while (selectedArray.length < inForm.Branch.options.length) {
inForm.Branch.options[(inForm.Branch.options.length - 1)] = null;
}
for (var i=0; i < selectedArray.length; i++) {
eval("inForm.Branch.options[i]=" + "new Option" + selectedArray[i]);
}
if (inForm.Division.options[0].value == '') {
inForm.Division.options[0]= null;
if ( navigator.appName == 'Netscape') {
if (parseInt(navigator.appVersion) < 4) {
window.history.go(0);
}
else {         
if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
window.history.go(0);
            }
         }
      }
   }
}
</SCRIPT>
<%=writeArrays1%> 
<form name="form" action=""> 
<select name="Division" onChange="jsCatT1(document.form,document.form.Division.options[document.form.Division.selectedIndex].value)"> 
<%
      sqlstmt = "SELECT * from bvu_distrito ORDER BY dist_descricao ASC; "
      set rs=Conn.execute(sqlstmt)
      if not rs.eof then
        do while not rs.eof
         optionValue = rs("dist_code")
          optionLabel = rs("dist_descricao")                                                                                                      
           response.write "<option value='" & optionValue & "'"
          'if cstr(optionValue) = cstr(xxx) then response.write " selected"
              response.write ">" & optionLabel & "</option>" & vbCrLf
          rs.MoveNext
        loop
      end if
      rs.close      
%> 
                      
</select> 
<select name="Branch">
</select> 
</form>

Open in new window

Great Help.

Very Thanks.

I have another question to you if its possible.

I need a third combo with have the value of the first with the value of second.

I have already made the arrays.

Can you see if its everthing good.

I don't know how made a function in javascript to the new combo.

Thanks,
Gui Reis
<!-- #include virtual="/conteudos/incSystem.asp"-->
<%
Server.ScriptTimeout=200
%>
<%
set conn=server.createobject("ADODB.Connection")
conn.open connection 
function writeArrays1()
            strARR1 = ""
            strARR2 = ""
            response.write "<SCRIPT LANGUAGE='JavaScript'>" & vbCrLf
            response.write "<!-- " & vbCrLf & "//Begin" & vbCrLf
            sqlstmt1 = "SELECT * from bvu_distrito  ORDER BY dist_code" 
                  set rs=conn.execute(sqlstmt1)
            if not rs.eof then
                  do while not rs.eof
                        x = 1
                        t1VarName = rs("dist_descricao")
                        t1VarNum  = rs("dist_code")            
                        t1VarAbbr = rs("dist_code")
                        strARR1   = strARR1 & "var _" & t1VarAbbr & "Array =  new Array(""('---------','',true,true)"","
                                                
                        sqlstmt2 = "SELECT * from bvu_concelho WHERE dist_code = " & t1VarNum & " ORDER BY conc_descricao ASC; "
                                    set rs2=conn.execute(sqlstmt2)
                        if not rs2.eof then            
                              do while not rs2.eof
                                    y = 1
                                    t2VarName = rs2("conc_descricao")                                    
                                    t2VarNum  = rs2("conc_code")
                                    if x > 1 then strARR1 = strARR1 & ""","
                                    strARR1 = strARR1 & """('" & t2VarName & "','" & t2VarNum & "')"                        
                                    x = x + 1
                              rs2.MoveNext
                              loop
                        end if
                        rs2.close()
                        strARR1 = strARR1 &  """);" & vbCrLf
                  rs.MoveNext
                  loop
            end if
            rs.close()            
            
            sqlstmt3 = "SELECT * from bvu_concelho ORDER BY dist_code, conc_code" 
            set rs3=conn.execute(sqlstmt3)
            if not rs3.eof then
                  do while not rs3.eof
                        w = 1
                        t3VarName = rs3("conc_descricao")
                        t3VarNum  = rs3("dist_code")            
                        t3VarNum2  = rs3("conc_code")            
                        t3VarAbbr = rs3("dist_code")
                        t3VarAbbr2 = rs3("conc_code")
                        strARR2   = strARR2 & "var _" & t3VarAbbr & "" & t3VarAbbr2 & "Array =  new Array(""('---------','',true,true)"","
                                                
                        sqlstmt4 = "SELECT * from bvu_freguesia WHERE dist_code = " & t3VarNum & " AND conc_code = " & t3VarNum2 & " ORDER BY freg_descricao ASC; "
                        set rs4=conn.execute(sqlstmt4)
                        if not rs4.eof then            
                              do while not rs4.eof
			                        z = 1
                                    t4VarName = rs4("freg_descricao")                                    
                                    t4VarNum  = rs4("freg_code")
                                    if w > 1 then strARR2 = strARR2 & ""","
                                    strARR2 = strARR2 & """('" & t4VarName & "','" & t4VarNum & "')"                        
                                    w = w + 1
                              rs4.MoveNext
                              loop
                        end if
                        rs4.close()
                        strARR2 = strARR2 &  """);" & vbCrLf
                  rs3.MoveNext
                  loop
            end if
            rs3.close()            
                        
            response.write strARR1 & vbCrLf            
            response.write strARR2 & vbCrLf            
       
            response.write "//End -->" & vbCrLf
            response.write "</script>" & vbCrLf
            
end function
%>
<html>
<head>
<meta http-equiv="Content-Language" content="pt">
<title><%=title%></title>
<link href="geral.css" rel="stylesheet" type="text/css">
</head>
<SCRIPT LANGUAGE="JavaScript">
function jsCatT1(inForm,selected) {
var selectedArray = eval("_"+selected + "Array");
while (selectedArray.length < inForm.Branch.options.length) {
inForm.Branch.options[(inForm.Branch.options.length - 1)] = null;
}
for (var i=0; i < selectedArray.length; i++) {
eval("inForm.Branch.options[i]=" + "new Option" + selectedArray[i]);
}
if (inForm.Division.options[0].value == '') {
inForm.Division.options[0]= null;
if ( navigator.appName == 'Netscape') {
if (parseInt(navigator.appVersion) < 4) {
window.history.go(0);
}
else {         
if (navigator.platform == 'Win32' || navigator.platform == 'Win16') {
window.history.go(0);
            }
         }
      }
   }
}
</SCRIPT>
<%=writeArrays1%> 
<form name="form" action=""> 
<select name="Division" onChange="jsCatT1(document.form,document.form.Division.options[document.form.Division.selectedIndex].value)"> 
<%
      sqlstmt = "SELECT * from bvu_distrito ORDER BY dist_descricao ASC; "
      set rs=Conn.execute(sqlstmt)
      if not rs.eof then
        do while not rs.eof
         optionValue = rs("dist_code")
          optionLabel = rs("dist_descricao")                                                                                                      
           response.write "<option value='" & optionValue & "'"
          'if cstr(optionValue) = cstr(xxx) then response.write " selected"
              response.write ">" & optionLabel & "</option>" & vbCrLf
          rs.MoveNext
        loop
      end if
      rs.close      
%> 
                      
</select> 
<select name="Branch">
</select> 
 
<select name="Freg">
</select> 
 
 
</form>

Open in new window

can you see the array in http://www.jf-sjtojal.pt/array.asp

The page are very slow i think it's because the large data from sql.
Instead of:
<select name="Branch">
</select>

Use this for your select:
<select name="Branch" onChange="jsCatT1(document.form, document.form.Division.options[document.form.Division.selectedIndex].value)+''+this.options[this.selectedIndex].value">
</select>
But i don'thave to do something in javascript function?
ASKER CERTIFIED SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

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
Exist un error can you see...

http://www.jf-sjtojal.pt/array.asp
The problem it's the browser.

It works on IE7

Thanks for all,

Gui Aragão Reis
Excelent