Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > HTML, XML, Javascript, AJAX

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2009, 12:07 AM   #1 (permalink)
praveenhathwar
Recruit
 
Join Date: Mar 2009
Posts: 2
praveenhathwar is on a distinguished road
Unhappy Populating Dropdown list based on the other dropdownlist

Hi Everyone,
I am new to his forum. I am developing an asp page using Javascript. I am not able to solve a problem i.e., I ve 2 dropdown lists. Based on the 1st dropdown list's data, second dropdownlist has to be populated (retrieving data from database). Can anyone help me in sorting this issue. Im posting my code below...




<HTML>
<HEAD>
<script language="JavaScript">
var clockID = 0;

function UpdateClock()
{
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
var tDate = new Date();
document.theClock.theTime.value = ""
+ tDate.getHours() + ":"
+ tDate.getMinutes() + ":"
+ tDate.getSeconds();

clockID = setTimeout("UpdateClock()", 1000);
}

function StartClock()
{
clockID = setTimeout("UpdateClock()", 500);
}

function KillClock()
{
if(clockID) {
clearTimeout(clockID);
clockID = 0;
}
}

function checkform()
{
if (document.form1.mlfrom.value=='')
{
alert('Please enter the name');
document.form1.mlfrom.focus();
return false;
}
else if (document.form1.cont.value=='')
{
alert('Please enter the Contact No / Extension');
document.form1.cont.focus();
return false;
}
else if (document.form1.comdesc.value=='')
{
alert('Please enter the complaint description');
document.form1.comdesc.focus();
return false;
}
return true;
}
function hidefield()
{
if(document.form1.location.selectedIndex == 0 )
{
document.form1.astno.disabled = true;
document.getElementById("A").style.display='block' ;
}
else if(document.form1.location.selectedIndex == 1 )
{
document.form1.astno.disabled = false;
document.getElementById("A").style.display='true';
}
}
function assests()
{

What code can i write here ? How can I connect to database within this function ? What is the right method ? Please guide me with alternate way if any ?

}
</script>


<TITLE>Company Name</TITLE></HEAD>
<BODY bgcolor=silver onload="StartClock()" onunload="KillClock()">
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.open "filedsn=C:\proj\Maintenance\mnd"

set rs2 = Server.CreateObject("ADODB.recordset")
sq = "Select * from division"
rs2.Open sq,conn
%>
<form name="theClock"><input type=text name="theTime" size=8></form>
<center><H1>Company Name</H1></center>
<center><H3>Complaint Register</H3></center>
<table align=center border=0>
<tr>
<td>From</td>
<td><form action="Regicomp.asp" method="post" id=form1 name=form1 onsubmit="return checkform();">
<input name=mlfrom></td>
</tr>
<tr>
<td>Nature of Complaint</td>
<td>
<select name="location" onchange="hidefield()">
<option selected>Non Production
<option>Production
</select>
</td>
</tr>
<tr>
<td>Division</td>
<td><select name="div" onchange="assests()">
<% Do while not rs2.EOF %>
<option><%Response.Write(rs2.Fields (1)) %></option>
<% rs2.MoveNext
Loop %>
</select>
</td>
<tr id="A">
<td>Asset Number</td>
<td><select name="astno">
<option> </option>
</select>
</td>
</tr>
<tr>
<td>Complaint Description</td>
<td><textarea name=comdesc rows=8 cols=40></textarea></td>
</tr>
<tr>
<td><input type = submit value="Register Complaint" name=regis></td>
<td><input type = reset value="Clear" name=cls></td>
</tr>
</form>
</table>
</BODY>
</HTML>
praveenhathwar is offline   Reply With Quote
Old 03-11-2009, 12:10 PM   #2 (permalink)
hexblot
Recruit
 
Join Date: Mar 2009
Location: Athens,Greece
Posts: 21
hexblot is on a distinguished road
I can't help you with ASP-specifics since I haven't done any, but I can tell you some general tips to point you the (hopefully) right way :

The nice and elegant way
Your output will be plain HTML -- hence, ASP can't help you directly since it's not running at the time.
What you need to do is write a simple Javascript function that connects to another page, and fetches the page output ( AJAX-style ). You can then just call the page with a few GET parameters like
http://mydomain.com/assests.asp?choice=1

That will call the assests.asp page, and give the parameter choice=1. That page can then connect to the database, do whatever processing is needed, and return the options for the next combobox to your Javascript.

Javascript then gets that output and dynamically fills the second combo box.

That's the nice, elegant, expandable way of doing things.

Edit: Just found a person who's done a small example of this here . It's in PHP but the PHP part is just an echo, the rest is Javascript

The quick and dirty
If your first combo box has only a couple of options though, and is only a quick work, you can just add as many hidden divs as possible options, and only show the one needed. Quick and dirty.

E.g. if your first combobox has 5 options, make 5 DIVs that are hidden. When the user picks one option from that combobox, unhide the appropriate DIV, and rehide the rest ( in case he changes options ).

Last edited by hexblot; 03-11-2009 at 12:13 PM. Reason: Found link with example
hexblot is offline   Reply With Quote
Old 04-13-2009, 01:23 AM   #3 (permalink)
praveenhathwar
Recruit
 
Join Date: Mar 2009
Posts: 2
praveenhathwar is on a distinguished road
Unhappy

After long time searching through the internet I got the solution... But I am facing another problem.

An asp page named (say, divisions.asp) is loaded initially which contains two more different pages (select.asp and asst.asp). Using Framesets I ve done it.

"select.asp" has 4 form fields and "asst.asp" has 3 fields along with submit button.

I ve to alert the user if he try to submit with blank values. I am able to trace the blank values and alert the user for one page ("asst.asp"). But i need both the pages to be checked... Can any one help me...


Here is my code...

----------------
Divisions.asp
----------------


Code:
<%@ Language=VBScript %>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Company Name - Compliant Register</TITLE>
</HEAD>
<FRAMESET rows="45%, 55%" border=0 noresize>
	<FRAME src=select.asp name=one>
    <FRAME src=asst.asp name=two>  
</FRAMESET>
</HTML>


----------------
Select.asp
----------------

Code:
<%@ Language=VBScript %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%
	Set conn=Server.CreateObject("ADODB.Connection")
	conn.open "filedsn=c:\maint\mnd"
%>

<script language="javascript">
<!--
function checkform()
{
    if (document.frmSelect.div.value=='')
	{
		alert('Please select the division');
		document.frmSelect.div.focus();
		return false;
	}
	return true;
}

function CheckKeyCode()
{
  if( (event.keyCode == 13 || event.keyCode == 9 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39) ||
	  (event.keyCode >= 48 && event.keyCode <= 57) || 
	  (event.keyCode >= 96 && event.keyCode <= 105) ) {
	return true; }
  else {
	return false;
  }
}

function hidefield()
{
     if(document.frmSelect.location.selectedIndex == 1 )
     { 
            document.frmSelect.astno.disabled = true;
            document.frmSelect.astno.selectedIndex=0;
            document.frmSelect.macname.value = '';
            document.frmSelect.macname.disabled = true;
            document.getElementById("A").style.display='block';
     }
     else if(document.frmSelect.location.selectedIndex == 0 )
     {
            document.frmSelect.astno.disabled = false;
            document.frmSelect.macname.disabled = false;
            document.getElementById("A").style.display='true';
     }
}

function dept_onchange(frmSelect) 
{
	frmSelect.submit(); 
}

//-->
</script>
<html>
<head>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<title>Complaint Register</title>
</head>
<body bgcolor=Ivory>
<TABLE WIDTH="100%" BORDER=0 CELLSPACING=0 CELLPADDING=0>
	<TR style="FONT-WEIGHT: bold; FONT-SIZE: 12px; FONT-FAMILY: verdana">
		<TD style="padding-left:220px;"><H2 style="COLOR: darkred; font-weight:Bold;">Company Name</H2></TD>
		<TD align=center><IMG src="Images/Logo1.bmp" align=center border=0></TD>
	</TR>
	<TR>
		<TD colspan=2 height=10></TD>
	</TR>
	<TR>
		<TD colspan=2 align=middle style="FONT-WEIGHT: bold; FONT-SIZE: 15px; COLOR: DarkSalmon; FONT-FAMILY: verdana"><U>Complaint Register</U></TD>
	</TR>
	<TR>
		<TD colspan=2 height=30></TD>
	</TR>
</TABLE>
<form name="frmSelect" method=post>
<TABLE WIDTH=100% ALIGN=left BORDER=0 CELLSPACING=0 CELLPADDING=0>
	<TR>
		<TD ALIGN=left width=170px style="FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: darkgreen; FONT-FAMILY: verdana">
			Select Division:
			&nbsp;<strong style="Color:Red">*</strong>
		</TD>
		<TD ALIGN=left style="padding-top:5px; FONT-SIZE: 12px; COLOR: black; FONT-FAMILY: verdana">
			<SELECT name=div LANGUAGE=javascript onchange="return dept_onchange(frmSelect)" style="width:150; FONT-SIZE: 12px; COLOR: black; FONT-FAMILY: verdana">
				<%	

				Set oRs=Server.CreateObject("adodb.recordset")
				strSQL = "SELECT DISTINCT Divname FROM division ORDER BY Divname"
				oRs.Open strSQL, conn		
				
				Response.Write "<option value=select Selected>-- Select --</option>"	
				Do while not oRs.EOF
					If Request.Form("div") = oRs("Divname") then 'if this is the selected one then display as selected
						Response.Write "<OPTION VALUE = '" & oRS ("Divname") & "' SELECTED>"
						Response.Write oRs("Divname") & "</Option>"
						oRs.MoveNext 
					Else
						Response.Write "<OPTION VALUE = '" & oRs ("Divname") & "'>"
						Response.Write oRs("Divname") & "</Option>"
						oRs.MoveNext 
					End if
				Loop		
				%>
			</SELECT>
		</TD>
	</TR>
	<TR>
		<TD ALIGN=left width=170px style="FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: darkgreen; FONT-FAMILY: verdana">
			Select Assest:
			&nbsp;<strong style="Color:Red">*</strong>
		</TD>
		<TD ALIGN=left style="padding-top:5px;">
			<SELECT name="astno" LANGUAGE="javascript" onchange="return dept_onchange(frmSelect)" style="width:150; FONT-SIZE: 12px; COLOR: black; FONT-FAMILY: verdana">
				<%		
				Set oRs=Server.CreateObject("adodb.recordset")
				strSQL = "SELECT * FROM assests where divname = '" & Request.form("div") & "'"
				oRs.Open strSQL, conn		
				
				Response.Write "<option value=select Selected>-- Select --</option>"
				Do while not oRs.EOF
					if Request.Form("astno") = oRs("asstno") then 'if this is the selected one then display as selected
						Response.Write "<OPTION VALUE = '" & oRS ("asstno") & "' SELECTED>"
						Response.Write oRs("asstno") & "</Option>"
						oRs.MoveNext 
					else
						
						Response.Write "<OPTION VALUE = '" & oRs ("asstno") & "'>"
						Response.Write oRs("asstno") & "</Option>"
						oRs.MoveNext 
					end if
				loop
				oRs.close 
				%>
			</SELECT>
		</TD>
	</TR>
	<TR>
		<TD ALIGN=left width=170px style="padding-top:3px; FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: darkgreen; FONT-FAMILY: verdana">
			Nature of Production:&nbsp;<strong style="Color:Red">*</strong>
		</TD>
		<TD ALIGN=left style="padding-top:5px; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<select name="location" onchange="hidefield()" style="FONT-SIZE: 12px; COLOR: black; FONT-FAMILY: verdana">
				<option selected>Production
				<option>Non Production</option>
			</select>
		</TD>
	</TR>
	<TR>
		<TD ALIGN=left width=170px style="padding-top:3px; FONT-WEIGHT: bold; FONT-SIZE: 12px; COLOR: darkgreen; FONT-FAMILY: verdana">Machine Name :</TD>
		<TD ALIGN=left style="padding-top:5px; FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<%		
				 If Request.form("astno") <> " " then
				    If Request.form("astno") <> "select" then
						Set oRs=Server.CreateObject("adodb.recordset")
						strSQL = "SELECT * FROM assests where asstno = '" & Request.form("astno") & "'"
						oRs.Open strSQL, conn		
						'Response.Write "<Input VALUE = '" & oRs ("macname") & "'>"
			%>
						<input name=macname size=40 value="<%=oRs("macname")%>">
				 <% Else %>
					<input name=macname size=40 value="-">
			  <% End if %> 
			<%	Else %>
					<input name=macname size=40 value="-">
			<%	End if %>
		</TD>
	</TR>
</TABLE>

<input type=hidden name="AssestNo" value=<%=Request.Form("astno")%>>
<% 
	session("div") = Request.Form("div")
	session("astno") = Request.Form("astno")
	session("location") = Request.Form("location")
%>
</form>
</body>
</html>

----------------
asst.asp
----------------


Code:
<%@ Language=VBScript %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<script language="JavaScript">
function checkform()
{
	if (document.assest.mlfrom.value=='')
	{
		alert('Please enter the name');
		document.assest.mlfrom.focus();
		return false;
	}
	else if (document.assest.cont.value=='')
	{
		alert('Please enter the Contact No./Extension No.');
		document.assest.cont.focus();
		return false;
	}
	else if (document.assest.comdesc.value=='')
	{
		alert('Please enter the complaint description');
		document.assest.comdesc.focus();
		return false;
	}
	else if (session("astno")=='select')
	{
		alert('Please select the assest number');
		return false;
	}
	else if (session("astno")=='-- Select --')
	{
		alert('Please select the assest number');
		return false;
	}
	else if (session("astno")=='')
	{
		alert('Please select the assest number');
		return false;
	}
	else if (session("div")=='')
	{
		alert('Please select the division');
		return false;
	}
	else if (session("div")=='select')
	{
		alert('Please select the division');
		return false;
	}
	else if (session("div")=='-- Select --')
	{
		alert('Please select the Division');
		return false;
	}
	return true;
}

function CheckKeyCode()
{
  if( (event.keyCode == 13 || event.keyCode == 9 || event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39) ||
	  (event.keyCode >= 48 && event.keyCode <= 57) || 
	  (event.keyCode >= 96 && event.keyCode <= 105) ) {
	return true; }
  else {
	return false;
  }
}
function hidefield()
{
     if(document.assest.location.selectedIndex == 1 )
     { 
            document.assest.astno.disabled = true;
            document.getElementById("A").style.display='block';
     }
     else if(document.assest.location.selectedIndex == 0 )
     {
            document.assest.astno.disabled = false;
            document.getElementById("A").style.display='true';
     }
}
</script>

<TITLE>Complaint Register</TITLE>
</HEAD>
<BODY bgColor=Ivory>
      <% 
         set conn=Server.CreateObject("ADODB.Connection")
         conn.open "filedsn=c:\maint\mnd"
         dim sif
         sif = session("astno")
     %>
<form action=Regicomp.asp method="post" id=assest name=assest target=_parent onsubmit="return checkform();">
<TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=0 CELLPADDING=0>
	<TR>
		<TD width=170px style="FONT-WEIGHT: bold; FONT-SIZE: 11px; COLOR: darkgreen; FONT-FAMILY: verdana" align=left>
			From:
			&nbsp;<strong style="Color:Red">*</strong>
		</TD>
		<TD align=left style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<input name="mlfrom">
		</TD>
	</TR>
	<TR>
		<TD width=170px style="FONT-WEIGHT: bold; FONT-SIZE: 11px; COLOR: darkgreen; FONT-FAMILY: verdana" align=left>
			Contact No:
			&nbsp;<strong style="Color:Red">*</strong>
		</TD>
		<TD align=left style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<input name="cont">
		</TD>
	</TR>
	<tr>
	    <td width=170px style="FONT-WEIGHT: bold; FONT-SIZE: 11px; COLOR: darkgreen; FONT-FAMILY: verdana">
			Complaint Description:
			&nbsp;<strong style="Color:Red">*</strong>
		</td>
		<td align=left style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<textarea name="comdesc" rows=5 cols=40></textarea>
		</td>
	</tr>
	<tr>
		<td align=left style="FONT-SIZE: 11px; COLOR: black; FONT-FAMILY: verdana">
			<input type=hidden name="Assest" value=<%=session("astno")%>>
		</td>
		<td>&nbsp;</td>
	</tr>
</TABLE>
<table border=0 bgcolor=Ivory>
	<tr>
		<td style="padding-left=25px;">
			<input type=submit value="Register Complaint" name=regis>
		</td>
		<td>
			<input type=reset value="Clear" name=cls>
		</td>
	</tr>
	<tr height=20>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td colspan=2 style="Font-weight:Bold; FONT-SIZE: 12px; COLOR: Red; FONT-FAMILY: verdana">
			Note: * marked fields should not be left blank
		</td>
	</tr>
	<tr>
		<td>
			<input type=hidden name=Contact value=<%=session("Qcont")%>>
		</td>
		<td>&nbsp;</td>
	</tr>
	<%
	session("from") = Request.Form("mlfrom")
	session("cont") = Request("cont")
	session("comdesc") = Request("comdesc")
	%>
</table>
</form>
</BODY>
</HTML>
praveenhathwar is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
list within in a list (lisp) coolman All Other Coding Languages 1 03-20-2006 10:42 PM
DropdownList event opens new browser window problem rikb53 MS Technologies ( ASP, VB, C#, .NET ) 2 02-25-2006 08:47 AM
sending dropdown list value as form parameter SeetaramaRaju HTML, XML, Javascript, AJAX 1 08-26-2005 07:02 PM
Populating tree control with specified directory folders and files HanaDin Platform/API C++ 2 07-14-2004 06:16 PM
Dropdown list in HTML JoeyJoeJo HTML, XML, Javascript, AJAX 8 02-26-2003 12:00 PM


All times are GMT -8. The time now is 06:52 PM.


Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0 RC8 ©2007, Crawlability, Inc.





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting