Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript_howto thread: How do I use JavaScript to write ASP to query a database


Message #1 by stephen_marrs@h... on Thu, 22 Aug 2002 08:59:14
Stephen, try that,
I assumed you have 5 fields in your table for
simplicity."BreedName,Size,Grooming,Exercise,Space"
You can modify according to the code i'm sending or
some part of the code..

Berkay...
<!----------------------------------------------------

<%@ LANGUAGE = JavaScript%>
<HTML>
<BODY
onLoad="SetValuesOnSwitch('<%=Request.Form("hidAttribChosen")%>');">
 <h3>Dog Breed Selector</h3>
 <p>Please choose what <label id="attribChosen"
name="attribChosen">Size</label> of dog you would
prefer:</p>
<SCRIPT LANGUAGE=javascript>
<!--
	function SizeDifferCases()
		{
				attrib1.innerHTML = 'Considerable' ;
				attrib2.innerHTML = 'Moderate' ;
				attrib3.innerHTML = 'Little' ;
				document.getElementById("rdBtn1").value 
'Considerable' ;
				document.getElementById("rdBtn2").value 
'Moderate' ;
				document.getElementById("rdBtn3").value = 'Little'
;
		}
	function ChangeRadios()
	  {
		var newField = frm1.select1;
		var fieldTxt 
newField.options[newField.selectedIndex].text ;
		SetValuesOnSwitch(fieldTxt);
	  }
	
	function SetValuesOnSwitch(param)
	  {
		switch (param)
		  {
			 case "Size" : { 
							attrib1.innerHTML = 'Small' ;
							attrib2.innerHTML = 'Medium' ;
							attrib3.innerHTML = 'Large' ;
							document.getElementById("rdBtn1").value 
'Small' ;
							document.getElementById("rdBtn2").value 
'Medium' ;
							document.getElementById("rdBtn3").value 
'Large' ;
							frm1.select1.options[0].selected = true;
							attribChosen.innerHTML = param ;
							break
						   }
			 case "Grooming" :
							{ SizeDifferCases(); 
							  frm1.select1.options[1].selected = true;
							  attribChosen.innerHTML = param ;
							  break
							}
			 case "Exercise" : 
						   { SizeDifferCases(); 
						     frm1.select1.options[2].selected = true;
						     attribChosen.innerHTML = param ;
						     break
						   }
			 case "Space" : 
						   { SizeDifferCases(); 
						     frm1.select1.options[3].selected = true;
						     attribChosen.innerHTML = param ;
						     break
			  			   }
		 }			 
	  }
//-->
</SCRIPT>
 <form method="post" name="frm1"
onSubmit="javascript:frm1.hidAttribChosen.value=attribChosen.innerHTML;frm1.hidChosen.value=attribChosen.innerHTML;">
 <input type="hidden" name="hidChosen" value='size'>
 <input type="hidden" name="hidAttribChosen"
value='size'>
 <table>
 <tr>
 <td>
 <SELECT id=select1 name=select1
onChange="javascript:ChangeRadios();">
	<OPTION>Size</OPTION>
	<OPTION>Grooming</OPTION>
	<OPTION>Exercise</OPTION>
	<OPTION>Space</OPTION>
</SELECT>
 </tr>
 <td><input type='radio' name='size' value='Small'
id="rdBtn1">
 <label id="attrib1">Small</label></td>
 <td><input type='radio' name='size' value='Medium'
id="rdBtn2">
 <label id="attrib2">Medium</label></td>
 <td><input type='radio' name='size' value='Large'
id="rdBtn3">
 <label id="attrib3">Large</label></td>
 <td><input type='submit' value='Find a Dog'
name='btnSubmit'></td>

<%
// Open connection to database, then populate a
recordset with list of 
//stock
   var requestItem = new
String(Request.Form("btnSubmit"));
   if (((requestItem.length) > 0) && (requestItem !
'undefined')) {
	   var adoConnection 
Server.CreateObject("ADODB.Connection");
	   var adoRecordSet;
	   var mySQL;
	   adoConnection.Open("DSN=DogDSN");
	   var mySQL = "SELECT BreedName FROM Breeds WHERE "
+ Request.Form("hidChosen") + 
				   "='" + Request.Form('size') + "'";
	   adoRecordSet = adoConnection.Execute(mySQL);
	   if  (!adoRecordSet.eof) {adoRecordSet.movefirst }
	// Loop through recordset and write Breed Names out
to page
	   while ( adoRecordSet.Eof == false )
	   {
	%>
	<TR>
	   <TD><%=adoRecordSet("BreedName").Value%></TD>
	   
	  
	  
	</TR>
	<%
	   adoRecordSet.MoveNext();
	   }

	// Close Recordset and connections 
	// and release memory used by Recordset and
Connection objects
	   adoRecordSet.Close();
	   adoRecordSet = null;
	   adoConnection.Close();
	   adoConnection = null;
   }
%>

<%Response.Write(Request.Form("size"))
%>

----------------------------------------------------->



--- stephen_marrs@h... wrote:
> Hello,Would somebody help me. I need to create a
> form that uses 
> ASP to allow users to choose between different
> criteria which will query 
> a db and return a list of suitable dog breeds to
> them using ASP and 
> JavaScript. The criteria are Size,Grooming,Exercise
> and Space and each of 
> these has the following attributes to choose
> from,e.g.Considerable
> (Con),Moderate(Mod) or Little(Lt)and in the case of
> size S,M,L and XL.
> This is what I have come up with so far
> 
> <%@ LANGUAGE = JavaScript%>
> <HTML>
> <BODY>
>  <h3>Dog Breed Selector</h3>
>  <p>Please choose what Size of dog you would
> prefer:</p>
>  <form>
>  <table>
>  <td><input type='radio' name='size'
> value='S'>Small</td>
>  <td><input type='radio' name='size'
> value='M'>Medium</td>
>  <td><input type='radio' name='size'
> value='L'>Large</td>
>  <td><input type='submit' value='Find a Dog'></td>
> 
> <%
> // Open connection to database, then populate a
> recordset with list of 
> stock
>    var adoConnection 
> Server.CreateObject("ADODB.Connection");
>    var adoRecordSet;
>    var mySQL;
>    adoConnection.Open("DSN=DogDSN");
>    var mySQL = "SELECT BreedName" + 
>       " FROM Breeds WHERE Size='" &
> Request.Form('size')&"'";
>    adoRecordSet = adoConnection.Execute(mySQL);
> 
> // Loop through recordset and write Breed Names out
> to page
>    while ( adoRecordSet.Eof == false )
>    {
> %>
> <TR>
>    <TD><%=adoRecordSet("BreedName").Value%></TD>
>    
>   
>   
> </TR>
> <%
>    adoRecordSet.MoveNext();
>    }
> 
> // Close Recordset and connections 
> // and release memory used by Recordset and
> Connection objects
>    adoRecordSet.Close();
>    adoRecordSet = null;
>    adoConnection.Close();
>    adoConnection = null;
> %>
> <%Response.Write(Request.Form("size"))
> %>
> 
> </BODY>
> 
> What would I need to change and add to this to meet
> my requirements?
> 
> 
> 
> 
> ---
> 
> Improve your web design skills with these new books
> from Glasshaus.
> 
> Usable Web Menus
>
http://www.amazon.com/exec/obidos/ASIN/1904151027/ref=nosim/theprogramme
> r-20
> Constructing Accessible Web Sites
>
http://www.amazon.com/exec/obidos/ASIN/1904151000/ref=nosim/theprogramme
> r-20
> Practical JavaScript for the Usable Web
>
http://www.amazon.com/exec/obidos/ASIN/1904151051/ref=nosim/theprogramme
> r-20


__________________________________________________
Do You Yahoo!?
HotJobs - Search Thousands of New Jobs
http://www.hotjobs.com

  Return to Index