Validationg against a selection

R

Robert Bravery

HI all,
I'm new to html and havascript. I want an alert box to apear when a user
selects a certain selection from a selet dropdownbox.
The select box is populated with data from a lookuptable from mysql. I have
assigned the table key to the value of the select box, and the description
column to the text of the select box. The table has one more column 'closed'
which is a int value of eother 1 or 0.
I would like the alert box to apear only when the users selects an option
that has a corresponding value of 1 in the 'closed' column.
Any help appreciated

Thanks
Robert
 
T

Thomas 'PointedEars' Lahn

Robert said:
[...] I want an alert box to apear when a user
selects a certain selection from a selet dropdownbox.
The select box is populated with data from a lookuptable from mysql. I
have assigned the table key to the value of the select box, and the
description column to the text of the select box. The table has one more
column 'closed' which is a int value of eother 1 or 0.
I would like the alert box to apear only when the users selects an option
that has a corresponding value of 1 in the 'closed' column.

Several things come to my mind. In order of preference:

1. Since the "closed" values appear to be forbidden ones, why serve them
in the first place?

2. The `option' element has a `disabled' attribute that can be used.
Disabled options cannot be selected.

<select ...>
...
<option value="2" disabled>bar (closed)</option>
...
</select>

You should test server-side whether a "closed" value was submitted
anyway.

3. Since the `option' element does not have an additional attribute that
can be used reasonably (user-defined attributes require XHTML or are
invalid markup, and unreliable anyway; the `click' event is not widely
supported for the `option' element), you would have to serve additional
script code that identifies a selected option as "closed". Such as:

...
<head>
...
<meta http-equiv="Content-Script-Type" content="text/javascript">
...
</head>

<body ...>
...
<form ...>
...
<script type="text/javascript">
var closed = [0, 1, 0, 0];
</script>

...
<select name="foobar" size="10"
onchange="if (closed[this.selectedIndex-1]) window.alert('...');">
<option value="0" selected>--- Select an option ---</option>
<option value="1">foo (open)</option>
<option value="2">bar (closed)</option>
<option value="3">baz (open)</option>
<option value="4">boo (open)</option>
</select>
...
</form>
...
</body>
...


HTH

PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top