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 Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Radio buttons to ASP written in JScript

Status
Not open for further replies.

finnoscar

Technical User
Aug 17, 2002
55
GB
How exactly would you add radio buttons to the script below in order to allow a user to choose attributes from a db table that has Size,Grooming,ExerciseRequirements(e.g. Size=Small,Medium or Large)etc.and return a Breed that meets their requirements.

<% @ Language = jscript %>
<%

function JScriptDatabaseRead() {
/*
declare variables
*/
var c, r, strOut = &quot;&quot;;
var sql = &quot;SELECT BreedName FROM Breeds WHERE Size = 'L' ORDER BY Size;&quot;;
var cnstr = &quot;&quot;;

var vbCrLf = String.fromCharCode( 13, 10 );

cnstr+= &quot;Provider=Microsoft.Jet.OLEDB.4.0;&quot;;

cnstr+= &quot;Data Source=&quot; + Server.Mappath(&quot;\DogBreeds1.mdb&quot;);

c = new ActiveXObject(&quot;ADODB.Connection&quot;);
c.Open(cnstr);
//c.Open( Application(&quot;dbConn&quot;), Application(&quot;dbUsr&quot;), Application(&quot;dbPass&quot;) );
r = c.Execute(sql);
while (!r.BOF & !r.EOF){
strOut+= r(0) + &quot;<BR>&quot; + vbCrLf;
r.MoveNext();
} // end while
/*
Close and free variables.
*/
r.Close();
c.Close();

/*
The return Statement exits the function and returns a value.

*/
return(strOut);
}
%>

<html>
<head>
<title>Dog Breed Selector</title>
</head>
<body >

<h3>Dog Breed Selector</h3>

From your responses to the questionnaire I believe that suitable possible breeds
for you would be:
<BR>
<BR>
<%
// call function and write the results to the browser.
Response.Write ( JScriptDatabaseRead() );
%>
</body>
</html>

I would be really grateful for help with this.
Thanks
 
The HTML part.
Code:
<form action='a_dog_for_you.asp' method='post'>
<input type='radio' name='size' value='Small'><br>
<input type='radio' name='size' value='Medium'><br>
<input type='radio' name='size' value='Large'><br>
<input type='submit' value='Size My Dog'>
</form>

The ASP part.
Code:
<%
function JScriptDatabaseRead() {
   if( Request.Form(&quot;size&quot;)() == &quot;&quot; ) {
      strOut = &quot;&quot;;
   } else {
   
   var sizeMyDog = Request.Form(&quot;size&quot;)();
    . . .
   var sql = &quot;SELECT BreedName FROM Breeds WHERE Size = '&quot; + sizeMyDog + &quot;' ORDER BY Size&quot;;

    . . . 
     
      /*
         The return Statement exits the function and    returns a value.
          
      */
       return(strOut);
   }
}
%>

How it works. The first time the script is requested there won't be any form data for size so no data is retrieved, strOut is empty, and just the radio buttons are written.

When the form is submitted there will be a value for size, assuming a radio button was clicked. This time strOut has the list of breeds of the size selected. The list is written together with the radio buttons.
 
Thanks
How would I go about adding other selection criteria to this.What I mean is something like
SELECT BreedName FROM Breeds WHERE Size=&quot;&quot; and Grooming=&quot;&quot; and Exercise=&quot;&quot; Etc.
 
You can set up a series of if stmts to build the query in pieces, I am rusty on javascript si I will write it as pseudo code/VB
Code:
Dim sqlStr, optFlag

sqlStr = &quot;SELECT BreedName FROM Breeds&quot;
optFlag = False

If Size <> &quot;&quot; Then
   sqlStr = &quot; WHERE Size = '&quot; & Size & &quot;'&quot;
   optFlag = True
End If

If Grooming <> &quot;&quot; Then
   If optFlag = True
      sqlStr = sqlStr & &quot; AND&quot;
   Else
      sqlStr = sqlStr & &quot; WHERE&quot;
   End If
   sqlStr = sqlStr & &quot; Grooming = '&quot; & Grooming & &quot;'&quot;
   optFlag = True
End If

If Exercise <> &quot;&quot; Then
   If optFlag = True
      sqlStr = sqlStr & &quot; AND&quot;
   Else
      sqlStr = sqlStr & &quot; WHERE&quot;
   End If
   sqlStr = sqlStr & &quot; Exercise = '&quot; & Exercise & &quot;'&quot;
   optFlag = True
End If

...etc

sqlStr = sqlStr & &quot; ORDER BY Size&quot;

Now this makes your ASP a little messy looking, your other option is to make your HTML Form on the previous page messy:
1) Make a counter
2) Call your entries name=attr<%=counter%> instead of size, grooming
3) Create a hidden field with each one that is named name=attrname<%=counter%> with the value equalt to the db field name it corresponds to
4) Incrememnt counter and continue back to 2 for each attribute

Now on the next page (page above) Instead of creating a seperate if statement for each attribute:
Code:
Dim sqlStr, optFlag, counter

sqlStr = &quot;SELECT BreedName FROM Breeds&quot;
optFlag = False

'start counter at 0 or 1 depending on what you started with on previous page

For counter = 0 to Max 'replace max with number of fields on previous page or put final value of counter into a hidden field on previous page and load it in here

   If Request.Form(&quot;attr&quot;&counter) <> &quot;&quot; Then
      If optFlag = True
         sqlStr = sqlStr & &quot; AND&quot;
      Else
         sqlStr = sqlStr & &quot; WHERE&quot;
      End If
      sqlStr = sqlStr & &quot; &quot; & Request.Form(&quot;attrname&quot;&counter) & &quot; = '&quot; & Request.Form(&quot;attr&quot;&counter) & &quot;'&quot;
      optFlag = True
   End If
Next

sqlStr = sqlStr & &quot; ORDER BY Size&quot;

This will loop through all of your attributes building your SQL String dynamically for every chosen attribute that was filled out on the previous page. This assumes your comparing only to text fields in the database, and that you placed the db field name correctly in the corresponding attrname field on the previous page.
-tarwn ------------ My Little Dictionary ---------
Reverse Engineering - The expensive solution to not paying for proper documentation
 
Many people do it that way.

I like to use indicator variables in my query like this.
I will show the query first as it might be submitted to the database. The symbols @anySize, @sizeMyDog, etc. stand for values that will be plugged in by your script.
Code:
SELECT BreedName FROM Breeds
WHERE ( @anySize OR Size = @sizeMyDog )
  AND ( @anyGrooming OR Grooming = @needsGrooming )
  AND ( @anyExercise OR Exercise = @frequencyExercise )

The indicator variables @anySize, @anyGrooming, and @anyExercise are set to true if no value is choosen. If the criterion matters and some value is choosen the indicator variable is set to false. So if size doesn't matter @anySize is true and that condition will be true for all values of Size.

So you still have to write the logic to set values for the indicator variables, but the query is always the same. This can give a performance advantage is you can store the query in the database.

Here is the code for setting one of the indicator variables.
If no radio button is clicked for Size there is no form data for sizeMyDog, it is empty, and the indicator variable is set to true, meaning any size is acceptable.
Code:
var anySize = false;
if(Request.Form(&quot;sizeMyDog&quot;) == &quot;&quot; ) {anySize = true;}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top