Forum Moderators: open

Message Too Old, No Replies

ASP Condition Based on Selected Form Field?

Easy or impossible to do? No idea.

         

abrown

11:46 pm on Aug 16, 2004 (gmt 0)

10+ Year Member



Hi,

Since I'm pretty new to programming, I have no idea whether it's impossible to do this...or extremely easy. Hopefully the latter!

I have a pretty standard form that registers people for some events. The first drop-down field contains the options for the event dates, including the option "Can't Attend." The second drop-down field contains options for their T-shirt size. Here's the rub: If they can't attend the event, they don't get a T-shirt.

So, is there a way to write a conditional statement before the form is submitted? IF they select the "Can't Attend" option in the first field, THEN the entire second "T-Shirt" field disappears?

Help would be much appreciated!

Just in case it matters, here's the form code:

<tr>
<td align="right" class="label">Event</td>
<td>
<select name="Event" id="0" class="select2" onMouseOver="handleMouseOver(this,myForm);">
<option value="">----- Please Select -----</option>
<option value="Lexington"<%=selected("Event","Lexington")%>>LexingtonPM</option>
<option value="Paramus"<%=selected("Event","Paramus")%>>Paramus</option>
<option value="King of Prussia<%=selected("Event","King of Prussia")%>>King of Prussia</option>
<option value="Gaithersburg"<%=selected("Event","Gaithersburg")%>>Gaithersburg</option>
<option value="Can't Attend"<%=selected("Event","Can't Attend")%>>Can't Attend</option>
</select>
</td>
</tr>

<tr>
<td align="right" class="label">T-Shirt Size</td>
<td>
<select name="Tshirt" id="1" class="select2" onMouseOver="handleMouseOver(this,myForm);">
<option value="">----- Please Select -----</option>
<option value="Large"<%=selected("Tshirt","Large")%>>Large</option>
<option value="Extra Large"<%=selected("Tshirt","Extra Large")%>>Extra Large</option>
<option value="Extra Extra Large"<%=selected("Tshirt","Extra Extra Large")%>>Extra Extra Large</option>
</select>
</td>
</tr>

txbakers

11:53 pm on Aug 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a number of ways to do this, all of which require a little coding, nothing too difficult.

You can use client side javascript (fastest method) which will hide the second drop down box if they select "Can't Attend".

You can also hide the second one from the start, and only show it if the select something other than "can't attend".

You can also do it on the server side, and if the first box was "can't attend" then you just ignore or give an alternate value for the second field.

Not that difficult, and I think would enhance usability.

abrown

12:06 am on Aug 17, 2004 (gmt 0)

10+ Year Member



Thanks txbakers,

I think the first option is what I'm looking for. I know it's not the prettiest or most friendly way, but I really want to avoid doing things on the server side as much as possible because I don't want to mess up how things are submitted to our CRM DB, etc.

This JavaScript method. Do you (or anybody else out there?!) know of a sample script I could use to achieve this? I tried searching the web, but I wasn't quite sure what I was looking for. I saw a lot about "onChange" but I couldn't find an example that worked specifically for my situation.

Thanks much!
-Alison

txbakers

3:22 am on Aug 17, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll want to pick a different value for the "Can't Attend" choice, because the single quote/apostrophe can and will wreak havoc with the javascript, and ultimately the SQL database if you're not careful.

Best to avoid it if you can.

I'll see if I can post something to help you.

abrown

4:11 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



Hadn't thought of that. I changed the option to "Cannot Attend." If you can't get around to posting something...maybe you could give me a better idea of what I should search for?

Thanks much!

abrown

11:17 pm on Aug 17, 2004 (gmt 0)

10+ Year Member



It took quite a while, but I found some code to modify to give me "basically" what I'm looking for. Just thought I'd share some snippets...

....
<script language="JavaScript" type="text/JavaScript">
function showother(){
if(document.forms[0].Event.value=="Cannot Attend, Still Interested"){
document.getElementById("otherdiv").style.display = "none"
}else{
document.getElementById("otherdiv").style.display= "block"
}
}
</script>
</head>
<body>
<table>
<form method="post" name="myForm">
<tr>
<td align="right" class="label">Event</td>
<td>
<select name="Event" id="0" onChange="showother()">
<option value="">----- Please Select -----</option>
<option value="Lexington"<%=selected("Event","Lexington")%>>Lexington MA</option>
<option value="Paramus"<%=selected("Event","Paramus")%>>Paramus NJ</option>
<option value="King of Prussia"<%=selected("Event","King of Prussia")%>>King of Prussia PA</option>
<option value="Gaithersburg MD"<%=selected("Event","Gaithersburg MD")%>>Gaithersburg MD</option>
<option value="Cannot Attend, Still Interested"<%=selected("Event","Cannot Attend, Still Interested")%>>Cannot attend, but still interested</option>
</select>
</td>
</tr>
<tr>
<td class="label" colspan="2" align="right">
<div id="otherdiv" style="display:none">
T-Shirt Size
<select name="Tshirt" id="1">
<option value="">----- Please Select -----</option>
<option value="L"<%=selected("Tshirt","L")%>>Large (L)</option>
<option value="XL"<%=selected("Tshirt","XL")%>>Extra Large (XL)</option>
<option value="XXL<%=selected("Tshirt","Extra Extra Large")%>>Extra Extra Large (XXL)</option>
</select>
</div>
</td>
</tr>
</form>
<\table>
...