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

Use JS to build query based on radio button selection

Status
Not open for further replies.

TallOne

Programmer
May 14, 2004
164
US
Ok.. I need a little help with this js problem. And this may not even be possible or I may be way off. I suspect the latter. I have two radio buttons. I want one to be selected by default when page first loads. When the other one is selected, I would like to submit the page to itself and select a sql query based upon which button was selected. I just can't seem to get this to work or the buttons to persist! Can someone point me in the right direction????

<script LANGUAGE="JAVASCRIPT">
function fnItemSelect()
{
if (document.form1.myItemSelect == 'P')
(document.form1.myItemSelect == 'P';
document.form1.submit();}
else
{document.form1.myItemSelect == 'K';
document.form1.submit();}
}
</SCRIPT>

...

<%
If Request.Form("myItemSelect") = "" Then
Response.Write "<input type=""hidden"" name=""myItemSelect"" value=""P"">"
Else
Response.Write "<input type=""hidden"" name=""myItemSelect"">"
End If
%>
......
</option></SELECT><INPUT type="radio" OnClick="fnItemSelect();" name=B1 value="product"
<%If Request.Form("myItemSelect") = "P" Then%>checked<% End If %>><font face=verdana size=1>Product
<INPUT type="radio" OnClick="fnItemSelect();" name=B1 value="kit"
<%If Request.Form("myItemSelect") = "K" Then%>checked<% End If %>><font face=verdana size=1>Kit

<%
If Request.Form("myItemSelect") = "K" Then
strSQL = This query
Else
strSQL = That query
End If
rs.Open strSQL
.....
%>
 
Try this approach:
Code:
<script LANGUAGE="JAVASCRIPT" type="text/javascript">

var defaultselect = '<%=Request.Form("myItemSelect")%>';

function fnItemSelect()
{

if (document.form1.myItemSelect.value != defaultselect)
  {   
  document.form1.submit();
  }

}
</script>

This will save the original value in a Javascript variable, then compare that to the value of the radio button clicked. If the radio button that contains the same value is clicked, nothing will happen. The first time the page loads, there will be neither radio button value in the JS variable, which means clicking either radio button will submit the form.

Lee
 
Hi Everyone,
Below is what I ended up doing. Just thought I would share. Thanks for your input Lee.

TallOne

Code:
<%
If Request.QueryString("formid") > "" Then Session("ItemSelect") = "P"
If Request.Form("optSelect") = "product" or Request.Form("optSelect") = "" Then
	Session("ItemSelect") = "P"
Else
	If Request.QueryString("formid") = "" Then Session("ItemSelect") = "K"
End If
%>
<font face="arial" size="1" style="color:#0000FF;">
	<input type="radio" name="optSelect" onClick="document.ProductPricingList.submit();" value="product" <%If Session("ItemSelect") <> "K" Then Response.Write "checked" End If%>>Product
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
	<input type="radio" name="optSelect" onClick="document.ProductPricingList.submit();" value="kit" <%If Session("ItemSelect") = "K" Then Response.Write "checked" End If%>>Kit
</font>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top