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!

Validate select elements...

Status
Not open for further replies.

jianhua

Programmer
Jun 22, 2001
55
US
Given:
1. query authors and books from database.
2. display the results in select drop-down boxes with <cfoutput query=&quot;queryName&quot; group=&quot;groupName&quot;>
3. the default of each drop-down box is blank.

Question: How can I validate the select drop-down boxes and only one book can be selected when submiting the form?

Here is the code:
<cfquery name=&quot;queryName&quot; datasource=&quot;…&quot;>
select author, book
from tableName

</cfquery>

/* display the results grouped by authors and all the books each author wrote are in a drop-down box */
<form action=&quot;actionPage&quot; method=&quot;post&quot; name=&quot;formName&quot;>
<cfoutput query=&quot;queryName&quot; group=&quot;author&quot;>
<b>#author#</b><br>
<select name=&quot;book&quot;>
<option value=&quot;&quot;></option>
<cfoutput group=&quot;book&quot;>
<option value=&quot;#book#&quot;>#book#</option>
</cfoutput>
</select>
</cfoutput>
<input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;View this book&quot;>
</form>

/* when viewing the source, it looks like the following */
Author1
<select name=&quot;book&quot;>
<option value=&quot;&quot;></option>
<option value=&quot;author1_book1>author1_book1 </option>
<option value=&quot;author1_book2>author1_book2 </option>
<option value=&quot;author1_book3>author1_book3</option>
</select>
Author2
<select name=&quot;book&quot;>
<option value=&quot;&quot;></option>
<option value=&quot;author2_book1>author2_book1 </option>
<option value=&quot;author2_book2>author2_book2 </option>
<option value=&quot;author2_book3>author1_book3</option>
</select>
Author3

<input type=&quot;submit&quot;...>
...

You can see from the source that all form elements &quot;select&quot; have the same name &quot;book&quot;. How can I validate them? I hope to write a javascript function to validate them and only one book can be selected when submitting the form.

Any suggestions…

Thanks in advance…
 
When you say only one book selected do you mean for each author or just one book on the whole page?

To force only one drop down with a selected book to be submmited have each one in a seperate form. Even if the forms all have the same name only one will be submmited.

Have a seperate submit button for each form so the user is aware that only one book can be submmited.

You could use 'this' when validating the forms so in the form tag you could have :

onsubmit=&quot;validate(this)&quot; which passes just the relevant form to the validation function you have.



HTH

KOla
 
One book on the whole page. Never mind about it. I have figured it out. Thanks anyway.

jianhua
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top