Gossamer Forum
Home : Products : DBMan : Customization :

Help on two level search forms

Quote Reply
Help on two level search forms
Evening all. Strange question.

How would one go about providing a second screen for searching? For example. You choose a State from the dropdown and then either one of two things can happen.

1. A drop down menu appears with the Cities relating to the State chosen

or

2. The next screen has a drop down list of the Cities based on the State chosen.

Any hints, advice, code on this idea?

Thanks
Theri

Quote Reply
Re: Help on two level search forms In reply to
You mean something like this?

http://www.wsabstract.com/script/cut183.shtml

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Help on two level search forms In reply to
Mark,

Exactly what I need....but how would I implement the build_select_field_from_db?

Would it be easy?

Thanks for the great link.

Theri


Quote Reply
Re: Help on two level search forms In reply to
Hey MAGSGQ,

I had a crack at it and came up with the following. I really hope it's what you wanted because it took me a while to figure out.

Modifying build_select_field_from_db would have been too hard, so I've made a new sub-routine instead.
Copy the new sub-routine into html.pl and use the code &combo_box; whenever you want to display it.

You will need to configure 3 things which are in red:
- Change STATE# to the number of your "State" field
- Change CITY# to the number of your "City" field

Anyway, here it is (apologies for the horizontal scrolling):

Code:
sub combo_box {
# --------------------------------------------------------
# Combo box
#
my $count = 0;
my (%states, @line, $first);
print '<form name="combobox"><select name="state" onChange="redirect(this.options.selectedIndex)">';
open (DB, "<$db_file_name") or &cgierr($!);
if ($db_use_flock) { flock(DB, 1); }
while (<DB>) {
@line = &split_decode($_);
$states{$line[STATE#]}->{$line[CITY#]} = '';
$first = $line[STATE#] unless ($first);
$count++;
}
close DB;

foreach $state (sort(keys(%states))) {
print '<option>' . $state;
}

print "</select>\n<select name=\"city\">";

foreach $city (sort(keys(%{$states{$first}}))) {
print '<option>' . $city;
}

print qq~
</select>
<input type="button" name="test" value="Go!" onClick="go();">
<script language="javascript">
<!--
var groups = document.combobox.state.options.length;
var group = new Array(groups);
for (j=0;j<groups;j++) { group[j]=new Array(); }
~;

my ($count1, $count2) = 0;
foreach $state (sort(keys(%states))) {
$count2 = 0;
foreach $city (sort(keys(%{$states{$state}}))) {
print qq~group[$count1][$count2]=new Option("$city","$city")\n~;
$count2++;
}
$count1++;
}
print qq~
var form = document.combobox;
var temp = form.city;

function redirect(x) {
for (m=temp.options.length-1;m>0;m--) { temp.options[m] = null; }
for (j=0;j<group[x].length;j++){ temp.options[j] = new Option(group[x][j].text,group[x][j].value); }
temp.options[0].selected = true;
}

function go() {
var state = form.state.options[form.state.selectedIndex].text;
var city = form.city.options[form.city.selectedIndex].text;
var url = '$db_script_link_url&view_records=1&State='+state+'&City='+city;
location = url;
}
//-->
</script>
~;
}
Let me know how it goes.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Help on two level search forms In reply to
Hey Mark.

I think it is perfect........just have to figure now where I can stick it. http://www.coastcottages.com/cgi-bin/dbman/db.cgi?db=default&uid=default&view_short_search=1

I am getting a runtime error at line 35, and not sure what to make of it.

BTW: Thanks again....

Theri



http://www.coastcottages.com/html.pl.txt
Quote Reply
Re: Help on two level search forms In reply to
I think I need some guidance on this one. Not sure what to do with the code :)



Quote Reply
Re: Help on two level search forms In reply to
Woops, sorry that's my fault, I left some debugging stuff in the code.

Change:
function go() {
...
alert(url);
//location = url;}

//-->
</script>


To:
function go() {
...
location = url;
}

//-->
</script>


I've updated my earlier post too.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Help on two level search forms In reply to
http://www.coastcottages.com/cgi-bin/dbman/db.cgi?db=default&uid=default&view_short_search=1


Still not co-operating for me


Here is the code I currently have:


sub combo_box {
# --------------------------------------------------------
# Combo box
#
my $count = 0;
my (%states, @line, $first);
print '<form name="combobox"><SELECT name="State" onChange="redirect(this.options.selectedIndex)">';
open (DB, "<$db_file_name") or &cgierr($!);
if ($db_use_flock) { flock(DB, 1); }
while (<DB>) {
@line = &split_decode($_);
$states{$line[5]}->{$line[4]} = '';
$first = $line[5] unless ($first);
$count++;
}
close DB;

foreach $state (sort(keys(%states))) {
print '<option>' . $state;
}

print "</select>\n<select name=\"City\">";

foreach $city (sort(keys(%{$states{$first}}))) {
print '<option>' . $city;
}
print qq~
</SELECT>
<input type="button" name="view_records" value="Go!" onClick="go()";></form>
<script language="javascript">

</script>
~;
}


Quote Reply
Re: Help on two level search forms In reply to
There still seems to be a problem with your code output:

function go() {
var state = form.state.options[form.state.selectedIndex].text;
var city = form.city.options[form.city.selectedIndex].text;
var url = "http://www.coastcottages.com/cgi-bin/dbman/db.cgi?db=default&uid=default&view_records=1&State="+state+"&City="+city;
//location = url;}
//-->
</script>


Take out those red slashes.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Help on two level search forms In reply to
Hey Mark. I just want to thank you for the help.

Still no luck. When I load this URL
http://www.coastcottages.com/cgi-bin/dbman/db.cgi?db=default&uid=default&view_short_search=1
I get this error message: Error: 'document.combobox.state.options' is not an object. Do you wish to debug? I click on cancel and the page loads. I took out those slashes as suggested. I know it is something small, but my skills in coding are somewhat novice like.

Theri

Quote Reply
Re: Help on two level search forms In reply to
The names of the select fields have been title cased for some reason:

<select name="State" onChange="redirect(this.options.selectedIndex)">
...
print "</select>\n<select name=\"City\">";


They need to be lowercase. Don't worry if that's not how they appear in your database definition, it only affects the JavaScript, not the CGI.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Help on two level search forms In reply to
Hey Mark.

Well I believe that I have it working. Not sure to what extent but I think it is.

http://www.coastcottages.com/cgi-bin/dbman/db.cgi?db=default&uid=default&view_short_search=1

Check it out and let me know if there are any changes you would make. FYI: the cosmetic look is just testing, so everything will change once this code is solidified.

Thanks again
Theri