Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with ASP and Form in JavaScript 1

Status
Not open for further replies.

finnoscar

Technical User
Aug 17, 2002
55
0
0
GB
Could somebody help me sort this out. I need to add radio buttons to this script so that a user would have a choice of Small,medium or large and have the list of dogs meeting their choices returned as an ASP. I would be really grateful for as much help as possible

<%@ LANGUAGE = JavaScript%>
<HTML>
<BODY>
<TABLE BORDER=1>
<THEAD>
<TR>
<TH>Breed Name</TH>


<TH></TH>
</TR>
</THEAD>

<%
// Open connection to database, then populate a recordset with list of stock
var adoConnection = Server.CreateObject(&quot;ADODB.Connection&quot;);
var adoRecordSet;
var mySQL;
adoConnection.Open(&quot;DSN=DogDSN&quot;);
var mySQL = &quot;SELECT BreedName&quot; +
&quot; FROM Breeds&quot;;
adoRecordSet = adoConnection.Execute(mySQL);

// Loop through recordset and write Breed Names out to page
while ( adoRecordSet.Eof == false )
{
%>
<TR>
<TD><%=adoRecordSet(&quot;BreedName&quot;).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;
%>
</TABLE>
</BODY>
</HTML>
 
<%@ LANGUAGE = &quot;JavaScript&quot;%>
<html>
<body>
<table border=&quot;1&quot;>
<%var str_DogSize = new String(Request.Form(&quot;DogSize&quot;));
if (str_DogSize!=&quot;undefined&quot; && !isNaN(str_DogSize) && parseInt(str_DogSize)>0){
%> <tr>
<td align=&quot;left&quot; style=&quot;font-weight:bold;&quot;>Breed Name</td>
</tr>
<% // Open connection to database, then populate a recordset with list of stock
var adoConnection = Server.CreateObject(&quot;ADODB.Connection&quot;);
adoConnection.Open(&quot;DSN=DogDSN&quot;);
var mySQL = &quot;SELECT BreedName FROM Breeds &quot;;
mySQL += &quot;where BreedSize=&quot; + parseInt(str_DogSize);
var adoRecordSet = adoConnection.Execute(mySQL);
// Loop through recordset and write Breed Names out to page
while (!adoRecordSet.Eof){
%> <tr>
<td><%=adoRecordSet.Fields(&quot;BreedName&quot;).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;
}else{
%> <tr>
<td valign=&quot;top&quot; align=&quot;right&quot;>Breed Size:&nbsp;</td>
<td align=&quot;left&quot;>
<form method=&quot;post&quot;>
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;1&quot;>Small<br>
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;2&quot;>Medium<br>
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;3&quot;>Large<br>
<input type=&quot;submit&quot; value=&quot;Search&quot;>
</form>
</td>
</tr>
<%}
%> </table>
</body>
</html> codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
You're not a complete programmer unless you know how to guess.
I hope I never consider myself an 'expert'.
<insert witticism here>
 
When I ran this I got the following message. What could be wrong with it.

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80040E10)
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 1.
/PickADog.asp, line 15
 
Also how can I add further radio buttons so that the list of breeds produced is based on more criteria than just size.The other attributes of each breed in the db are Grooming,Exercise,Feeding and Space and they are all described as either little(Lt),Moderate(Mod) or Considerable(Con).
 
Could somebody help as I need this urgently.
 
Whenever I run it now and pick a radio button and submit it it just clicks and the button clears and nothing happens. How do I get it to display the results.HELP,PLEASE!
 
Regarding the error - show the database query ...
I'll sort out the rest when I wake up. codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
You're not a complete programmer unless you know how to guess.
I hope I never consider myself an 'expert'.
<insert witticism here>
 
// Open connection to database, then populate a recordset with list of stock
var adoConnection = Server.CreateObject(&quot;ADODB.Connection&quot;);
adoConnection.Open(&quot;DSN=DogDSN&quot;);
var mySQL = &quot;SELECT BreedName FROM Breeds &quot;;
mySQL += &quot;WHERE Size=&quot; + parseInt(str_DogSize);
L15> var adoRecordSet = adoConnection.Execute(mySQL);
// Loop through recordset and write Breed Names out to page
while (!adoRecordSet.Eof){

As ar as I can see,which is admittedly not very far that line 15 where the error occurs.Thanks
 
Could you tell me how exactly you you add more radio buttons so that the list of breeds is based on more than just size
 
Assuming that sizes are also measured in the database as 'Lt','Mod' and 'Con', the above example I gave will not work. Just substitute the values you wish to pass to the database, in the VALUE attribute of the INPUT tags.
e.g.
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;Lt&quot;>Little<br>
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;Mod&quot;>Moderate<br>
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;Con&quot;>Considerable<br>

and given they ('lt','mod','con') are STRINGS you'll need to surround them in quotes for the query to work. Given you're using an Access database, I'd suggest double quotes, and given you're using javascript (which can use either quote for literal containment) I'd change the quotes surrounding you whole query to single.
e.g.
var mySQL = 'SELECT BreedName FROM Breeds ';
mySQL += 'where BreedSize=&quot;' + parseInt(str_DogSize) + '&quot; ';

Also given we're dealing with strings not numbers, you can simplify the IF statement at the top to:
if (str_DogSize!=&quot;undefined&quot;){

As to adding more categories of radio buttons .. fairly simple:
* Copy the radio button code (between <tr></tr> inclusive) and paste it directly below the </tr>.
* Change the second set of radio buttons' NAME attributes to &quot;DogGrooming&quot;
* Add another variable at the top to catch the DogGrooming Form element.
* Add a check for the new variable to the IF statement (in javascript separate with && for AND):
if (str_DogSize!=&quot;undefined&quot; && str_DogGrooming!=&quot;undefined&quot;){
* Add a line to the query:
mySQL += 'and Grooming=&quot;' + str_DogGrooming + '&quot; ';

You can repeat the above for as many categories as needed. codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
You're not a complete programmer unless you know how to guess.
I hope I never consider myself an 'expert'.
<insert witticism here>
 
Thanks a million,codestorm
But could you tell me do I declare a new var string for each of the subsequent categories e.g
var str_DogGrooming = new String(Request.Form(&quot;DogGrooming&quot;));
and do you put whatever values you have in the db in the VALUE attribute of the INPUT tags e.g. Size = S,M,L,XL and Grooming = Lt,Mod and Con,etc.
These are probably stupid questions but I'm very new to programming. Thanks again you've been a real lifesaver.
 
Also I forgot to ask but can I remove the submit buttons and have just one at the bottom. Thanks
 

Because they are strings and not numbers do I need the parseInt in the statement below
var mySQL = &quot;SELECT BreedName FROM Breeds &quot;;
mySQL += &quot;where BreedSize=&quot; + parseInt(str_DogSize);

or would

var mySQL = 'SELECT BreedName FROM Breeds ';
mySQL += 'where BreedSize=&quot;' + str_DogSize + '&quot;';
 
try using the CStr function for string conversions
var mySQL = &quot;SELECT BreedName FROM Breeds &quot;;
mySQL += &quot;where BreedSize=&quot; + CStr(str_DogSize);
Hope that helps
admin@onpntwebdesigns.com
 
sorry jsut realized this was javascript I'm wrong don't listen to me too early [lol] Hope that helps
admin@onpntwebdesigns.com
 
Do I have to add a Response.Write command to the script to get it to print out and if I do how do I?
 
Or is the problem that I have the form in the same place as the JavaScript that is supposed to retrieve the details
because whenever I make my selections and prees submit the radio buttons just clear and nothing is returned
 
Could somebody please explain to me how this is supposed to work because I need this Fairly urgently. Thanks
 
Could Someone please help me with these questions as I need to know how it is supposed to print the list of breeds out to the page. Thanks.
 
>could you tell me do I declare a new var
>string for each of the subsequent
>categories e.g var str_DogGrooming = new
>String(Request.Form(&quot;DogGrooming&quot;));

yes

>and do you put whatever values you have in
>the db in the VALUE attribute of the INPUT
>tags e.g. Size = S,M,L,XL and Grooming =
>Lt,Mod and Con,etc.

yes, and be sure to enclose them in quotes
e.g.
<input type=&quot;radio&quot; name=&quot;DogSize&quot; value=&quot;Con&quot;>Considerable<br>

>Also I forgot to ask but can I remove the
>submit buttons and have just one at the
>bottom. Thanks

you should only have one submit button there in any case - not sure why you've ended up with many.

>Because they are strings and not numbers do
>I need the parseInt

you are correct in your code below
var mySQL = 'SELECT BreedName FROM Breeds ';
mySQL += 'where BreedSize=&quot;' + str_DogSize + '&quot;';

if it's not working, try adding a line somewhere like
Response.write(Request.Form);
to show you what has been submitted. from there you should be able to figure out if anything's missing. if it all seems in order, analyse the initial IF statement in the code.
codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
You're not a complete programmer unless you know how to guess.
I hope I never consider myself an 'expert'.
<insert witticism here>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top