Gossamer Forum
Home : Products : DBMan : Customization :

enter new value OR select from list

Quote Reply
enter new value OR select from list
I would like to enable a user to choose whether (a) they would add a field value by choosing from a dropdown list or (b) enter a new value not contained in the list in an ordinary text input field. The field itself is obligatory.

Is it possible to do this in one go? Obviously both the select field and the input text field would have to have the same name, and that doesn't work.

The only solution I can think of right now is to do this in steps:
- add record: show dropdown list for field.
- preview record: if no list item was selected, then show input text field for field

Alternatives? (Javascript?)

kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
Javascript would be your best bet. Let me know if you need a simple script for this.

Mike

Quote Reply
Re: [Watts] enter new value OR select from list In reply to
I do! Thanks a lot for the offer.
kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
In the FAQ there are instructions to do this without having to use javascript.

Under "Fields" there is a thread called "Select and Text name for same field?"
record ID: 945

Hope this helps



Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/
Quote Reply
Re: [LoisC] enter new value OR select from list In reply to
Thanks a lot for the reference.
Here, too, as in the multiple select field problem, my rewrite of parse_form causes some problems. Got some thinking to do.
kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
Here's the javascript part if you still need it, I started on it a couple of times but got sidetracked...

Code:
<html>
<body>

<script language="javascript">
function doSele (){
if ((document.form.bob.value == "Choose One") && (document.form.ralph.value == "")) {
alert("Must Choose Select or Enter Text");
document.form.ralph.select(); document.form.ralph.focus();
}

if (!(document.form.bob.value == "Choose One")) {
document.form.ralph.value = document.form.bob.value;
}

}
</script>

<form name="form">
<select name="bob">
<option value="Choose One">Choose One</option>
<option value="bob">bob</option>
</select>
<P><input type="text" name="ralph" value="">
<input type="button" onClick="doSele()">
</form>
</body>
</html>
The following bit works, note the form name is "form" if your form is named "timmy" or something then you need to use: document.timmy.bob.value

Try experimenting with it. Put the onClick="doSele()" part somewhere so it'll be clicked on (may not work with your submit button without some fussing with). Instead you can use 'onBlur' or 'onChange' or 'onFocus' and many other 'event handlers' elsewhere in your form.

Good luck, hope this helps!

Quote Reply
Re: [Watts] enter new value OR select from list In reply to
Thanks, this looks great, I'll give it a try.

In the meantime, I've fiddled around with sub parse_form, and as far as I can tell, this code can also deal with multiple fields with the same name, though it doesn't do any error-checking and therefore might miss out on cases where the user mistakenly selects from the list *and* types into the text field - I guess your javascript can be adapted to take care of error-checking.

Here's how far I got with parse_form:
Code:
...
use CGI qw(:standard :cgi-lib);
my $q = new CGI;
my %in = &parse_form;
...

sub parse_form {
my $input = $q->Vars;
my %in;
foreach my $key (keys %{$input}) {
next if ($input->{$key} eq "---");#empty select fields
next if (!$input->{$key});#empty fields
$in{$key} = $input->{$key};
$in{$key} =~ s/---//g;#if we have multiple fields and some are empty selects
$in{$key} =~ s/^\0//;# two fields with the same name and the first empty
$in{$key} =~ s/(\0)$//; # ... same with second empty
$in{$key} =~ s/\0/,/g;#for multiple select fields
}
return (%in);
}




kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
Plug this in to avoid having a selected item and a text item in the javascript:
Code:
if ((!(document.form.bob.value == "Choose One")) && (!(document.form.ralph.value == document.form.bob.value))) {
alert("Cannot Have Both");
document.form.ralph.select();
document.form.ralph.focus();
}


Quote Reply
Re: [Watts] enter new value OR select from list In reply to
Ah, well, now I see why I can't actually use this snippet - in my case, both fields have the same name. I don't know enough javascript to do this, but is it possible to test whether there are two fields with the same name, both of which have *some* input (which may or may not be identical, doesn't matter)?
kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
Good question... let me sleep on it and I'll see what I come up with over the (U.S.) Thanksgiving holiday.

Our office will be closed tomorrow (Thurs) and I plan on parking myself in front of the T.V. and vegging out.






Quote Reply
Re: [Watts] enter new value OR select from list In reply to
happy vegging!
kellner
Quote Reply
Re: [kellner] enter new value OR select from list In reply to
New improved code:

Code:
<script language="javascript">
function doSele (){
if ((document.form.bob.value == "Choose One") && (document.form.ralph.value == "")) {
alert("Must Choose Select or Enter Text");
document.form.bob.select();
document.form.bob.focus();
}
if ((!(document.form.bob.value == "Choose One")) && (!(document.form.ralph.value == ""))) {
alert("Cannot Have Both");
document.form.ralph.select();
document.form.ralph.focus();
}
if (!(document.form.bob.value == "Choose One")) {
document.form.sue.value = document.form.bob.value;
}
if (document.form.bob.value == "Choose One") {
document.form.sue.value = document.form.ralph.value;
}
}
</script>

<form name="form">
<INPUT TYPE="text" NAME="sue" VALUE="">
<select name="bob">
<option value="Choose One">Choose One</option>
<option value="bob">bob</option>
</select>
<P><input type="text" name="ralph" value="">
<input type="button" name="button" value="try it" onClick="doSele()">
</form>

Change sue's type to "hidden" and that'll be the field that goes into the database, the other two are 'dummy' fields simply used to set sue's value.

Good Luck!