Gossamer Forum
Home : Products : DBMan : Customization :

Select field depended of other

(Page 1 of 2)
> >
Quote Reply
Select field depended of other
Hi,
I saw another topic, that looks like my question:
I have a select field in my search display. What I will do is, when someone select an option in the first field, the second select field must be filled with the values of that second field, whitch all have the value of the first select field.
So when someone select a 'BMW', all the 'BMW's are found. The second field (model) must be loaded with all values of the 'model' field of that 'BMW' record as another select field. How can I do that ???
Quote Reply
Re: Select field depended of other In reply to
Check out www.javascriptsource.com for a script to do that. Unfortunately, I'm not sure whether or not you'll still be able to use print &build_select_field though.....
Quote Reply
Re: Select field depended of other In reply to
Thank you, web dog, but I have it running with JavaScript already, I just refer to the topic of 'Katana Man, a few lines below this, http://www.gossamer-threads.com/scripts/forum/resources/Forum12/HTML/000681.html ,maybe its to implant in the script.
Quote Reply
Re: Select field depended of other In reply to
It would have to be a Javascript script, because DBMan itself won't build select fields "on the fly."

It may be possible for the Javascript to use an array to fill out the select fields, but it would have to be an array/hash combination. (I can't remember whether it would be an array of a hash or a hash of an array.) Each element in the array would be in the form of "$data{'BMW'}[0]'. I don't know how to explain this, except to say that it's pretty complex.

I guess the question is whether the Javascript will use an array to build the select field.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
From a part of the 'html_search_options' the script build select fields out of the .cfg db_def. I think there must be a way, to change the code for take values instead out of the .cfg, take them out of the database file. The purpose is, that the users mostly get a succesful search-result. Here's the code out of the sub html_search_options:
Code:
<SELECT NAME="sb">
<OPTION>Selecteer
~; for (my $i =0; $i <= $#db_cols; $i++) { print qq~<OPTION VALUE="$i">$db_cols[$i]</OPTION>\n~ if ($db_form_len{$db_cols[$i]} >= 0);
}
print qq~
</SELECT>
Thanks in advance.
------
Mart.

[This message has been edited by mart (edited August 14, 1999).]
Quote Reply
Re: Select field depended of other In reply to
Sure. There's no problem with pulling values out of the database. Heck, you could just use &build_select_field_from_db and not have to do any coding at all.

The problem is
Quote:
when someone select a 'BMW', all the 'BMW's are found. The second field (model) must be loaded with all values of the 'model' field of that 'BMW' record as another select field.

I understood that to mean that you want the options in the "model" select field to change depending on what was selected in the "make" select field. Isn't that what you want? For example, if someone selects "BMW," you don't want "Corolla" to come up in the models field. Isn't that correct?

You can't do that without using Javascript or something else to change the "model" select field. Without some other sort of scripting, DBMan can't make changes to a form based on input in that same form.

You could do it if you had two separate forms:
Page 1 -- select the make of car. Hit the submit button.
Page 2 -- select the model of car from a list of models currently in the database that match the make you selected on Page 1.

I understand that you can have dynamic select lists if you use Javascript, but I don't know how you determine what the options of the select list will be. You might look at some of the Javascript sites and see how this feature works and then, if you have trouble populating the select fields, you can post the code here and we'll see what we can do to help.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
This isn't the full code you need, but I want to make sure it's will give you the lists you need.

Add the following code to the beginning of your subroutine. Be sure to change the first two lines to match the field numbers of your database.

Code:
$Merk_number = 5;
$Type_number = 6;
open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$Type_number], @{$Types{$fields[$Merk_number]}})) {
push (@{$Types{$fields[$Merk_number]}}, $fields[$Type_number]);
}
if (!(grep $_ eq $fields[$Merk_number], @Merks)) {
push (@Merks, $fields[$Merk_number]);
}
}
close DB;

Put the following code after the end of your form.

Code:
foreach $Merk (sort @Merks) {
print "$Merk<BR>";
foreach $type (sort @{$Types{$Merk}}) {
print " $type<BR>";
}
print "<BR>";
}

You should get a list of the makes and models of cars currently in your database, sorted and one on each line. Let me know what happens.

------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
Do I have to place it in the beginning of the searching sub or just in the first form I gave you and so, must the Java still be in there??
Quote Reply
Re: Select field depended of other In reply to
Not give up yet,
Quote:
I understood that to mean that you want the options in the "model" select field to change depending on what was selected in the "make" select field. Isn't that what you want? For example, if someone selects "BMW," you don't want "Corolla" to come up in the models field. Isn't that correct?
Yes, JPD, you completely understood what I mean.
I had it run once but the results not showsup correctly and I took the 'model' results out of a .txt file.
I have this running now on http://www.autorandstad.nl/index3, press 'Particulieren', than in the left bottom frame. Only the first 4 'merken' will work. My point is, that the results in the second field are here not depended on the db, so there are a lot of 'no result' searchings. I've worked quit a long time, to make this thing do what I want so maybe I can use it anyway. I think, I need a tiny little script, that will do what is nessecary, called after the 'merk' is selected.

I will, when a user select the 'name'(BMW) with an 'onChange' event there will be startup a NEW search routine, load this name in a variable, takes the model values depending of the 'name var' out of the db, resulting with the 'name' in the first field and the models in the second field to continue than with the search routine. Make sence???

[This message has been edited by mart (edited August 14, 1999).]
Quote Reply
Re: Select field depended of other In reply to
The only way that this will work is to do the search ahead of time.

Populating the variable and doing the search is not a problem. What I need you to do is to post the code for the Javascript which builds the dynamic select list so I can see what you need to do in order to put the correct values into the Javascript.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
This is the main.htm, I called it 'standaard.htm', its called from the start of the frameset.
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="GENERATOR" CONTENT="Mozilla/4.05 [nl] (Win95; I) [Netscape]">
<TITLE>standaard</TITLE>
<SCRIPT language="Javascript">
function jumpMerk(form){
var URL = form.Merk.options[form.Merk.selectedIndex].value+'.htm';
parent.frames[1].location.href= URL ;
return false;
}

</SCRIPT>
</HEAD>

<BODY BACKGROUND="../framelinkssmal1.jpg">
<FORM method="get" target="new" name="form">
<Font color="#FFFFFF"><b> Zoek SNEL!!</b>
<TABLE>
<TR>
<TD ALIGN=CENTER><SELECT name="Merk" size="1" onChange="jumpMerk(this.form)">
<OPTION selected value="standaard">Merk</OPTION>
<OPTION value="alfa">Alfa<OPTION value="audi">Audi<OPTION value="austin">Austin<OPTION value="BMW">BMW...(al other options)</OPTION></SELECT></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
And this is another file from the server, its called, when a user clicked on 'BMW', i called it 'BMW.htm':
Code:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Mart Kruijt (Automart)">
<META NAME="GENERATOR" CONTENT="Mozilla/4.05 [nl] (Win95; I) [Netscape]">
<TITLE>BMW</TITLE>

<Script language="Javascript">
<!-- Hide
function jumpMerk(form){

var URLM = form.Merk.options[form.Merk.selectedIndex].value+'.htm';
parent.frames[1].location.href = URLM ;
return true;
}

function jumpType(form){
if (form.Type.options[form.Type.selectedIndex].value ==0){
alert("Selecteer het model of druk\nop 'Go' voor alle modellen.")
return false;
}

var URLG = form.Merk.options[form.Merk.selectedIndex].value;
var TYPE = "&Type="
var ADRESS = "http://www.autorandstad.nl/cgi-bin/dbman/db.cgi?db=default&uid=default&view_records=1&ID=*&Merk=";
var URLT = form.Type.options[form.Type.selectedIndex].value;
top.frames[2].location.href= ADRESS + URLG + TYPE + URLT ;
return true;
}

function jumpGo(form){
if ( form.Type.options[form.Type.selectedIndex].value !=0){
alert("Er wordt al een zoekopdracht uitgevoerd!!")
return false;
}

else {
var URLG = form.Merk.options[form.Merk.selectedIndex].value;
var link = "http://www.autorandstad.nl/cgi-bin/dbman/db.cgi?db=default&uid=default&view_records=1&ID=*&Merk=";
top.frames[2].location.href= link + URLG;
return true;
}
}
// End Hide-->
</script>
</head>

<BODY BACKGROUND="../framelinkssmal1.jpg">
<FORM method="get" target="type" name="form">

<TABLE>
<TR>
<TD ALIGN=CENTER><SELECT name="Merk" size="1" onChange="jumpMerk(this.form)">
<OPTION selected value="standaard">Merk</OPTION>
<OPTION value="alfa">Alfa<OPTION value="audi">Audi<OPTION value="austin">Austin<OPTION value="BMW">BMW...(al other options)</OPTION></SELECT></TD><td>
<INPUT TYPE="button" value="Go" onclick="jumpGo(this.form)"></TD></tr>
<tr><td colspan=2><B><SELECT name="Type" size="1" onChange="jumpType(this.form)">
<OPTION selected value="">Selecteer
<OPTION value="316">316
<OPTION value="318">318
<OPTION value="320">320
<OPTION value="323">323
<OPTION value="325">325
<OPTION value="M3">M3
<OPTION value="518">518
<OPTION value="520">520
<OPTION value="523">523
<OPTION value="525">525
<OPTION value="528">528
<OPTION value="530">530
<OPTION value="535">535
<OPTION value="540">540
<OPTION value="628">628
<OPTION value="630">630
<OPTION value="635">635
<OPTION value="725">725
<OPTION value="728">728
<OPTION value="730">730
<OPTION value="735">735
<OPTION value="745">745
<OPTION value="750">750
<OPTION value="840">840
<OPTION value="850">850
<OPTION value="Z1">Z1
<OPTION value="Z3">Z3
</SELECT>
</td></tr></table>
</body></html>
but that last file should be loaded from the database. OK???


[This message has been edited by mart (edited August 31, 1999).]

[This message has been edited by mart (edited August 31, 1999).]
Quote Reply
Re: Select field depended of other In reply to
I can't follow this. I'll see what I can find on a javascript site that will make more sense to me.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
Place the code at the beginning of the searching subroutine. Whether you keep your
Javascript in there is up to you. I don't have any Javascript code as yet. I'm just
trying to figure out if this is the correct way to build some arrays, since I've never used this syntax before.

------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
We'll get the select field built once we're sure that the arrays are working correctly.

Are you saying that it lists multiples of the Types, like this:

Code:
BMW
Type1
Type1
Type2
Type3
Type3
Type3
Type3
Toytota
Celica
Celica
Celica
Corolla
Corolla

If this is what you're getting, I'll still need to work on the arrays a little bit.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
Yep,
no hurry,
I've got more serious problems with the private mailing mod...

Thank you.
Quote Reply
Re: Select field depended of other In reply to
I am only going to work on one modification per person at a time. You decide which one you want me to work on.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
Well, I think we're on the good way. It give a list, sorted by 'Merk' and under the name of the 'Merk' there is all the availeble 'Types'. It only has to be in an option-select display and the doubles 'Types' should be out.

Thanks...
--------
Mart.
Quote Reply
Re: Select field depended of other In reply to
At this time I prefer the mailing modification. I answered your comment that you gave me just before you posted this answere above. So, before I start with the things to delete for what I mention in that other topic, please can you take a look at my files???

Thanks in advance
--------
Mart.
Quote Reply
Re: Select field depended of other In reply to
(I saw your last two responses last night, just as I was ready to go to bed at 1am, and I was going to answer, but my server went offline. So I went to bed. Smile )

I'm not sure what the point of having a separate file would be, unless you are wanting the select lists to be on a static html page and you want to use an SSI. If that's what you want to do, someone else will have to help. I see "SSI" and I go completely blank. Smile

Okay. So the code works for finding the Makes of cars. Now you need to find the Models for each of the makes.

I'm going to have to go offline to work on this for a bit. I'll take the previous code that I wrote, come up with some sample data and see if I can get it to create the hashes and lists that we need.

After I answer any questions that were posted here since I started writing this and I answer the questions in the other forum, I'll get to working on this.



------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
I'm ready for this, I've tried something out, but the results are none.

Thanks in advance.
--------
Mart.
Quote Reply
Re: Select field depended of other In reply to
Okay. Let's try this one part at a time, shall we?

Just use this part of the previous code:

Code:
$Merk_number = 5; # field number that holds the make of the car

open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$Merk_number], @Merks)) {
push (@Merks, $fields[$Merk_number]);
}
}
close DB;

Then, put this at the end of your page, so we can see if the array is giving you what you want.

Code:
foreach $Merk (sort @Merks) {
print "$Merk<BR>";
}

It should give you the list of Makes of cars currently in your database.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
Wow, I wasn't expected you're online, very, very quick answere. Smile(here its 09.45 in the morning...)

First I'm trying to make this a seperate script, just like 'cleanup.pl. I only wonder, how do you call that script, just type 'http://my.server.com/db.cgi?makes_models.pl' ???

[This message has been edited by mart (edited August 31, 1999).]
Quote Reply
Re: Select field depended of other In reply to
This code gives a perfect sorting list of all the makes availeble in the db. Smile Smile.
Quote Reply
Re: Select field depended of other In reply to
I just ran the original code I wrote through Perl on my home computer and it worked like a charm.

Let's try it again, shall we?

Code:
$Merk_number = 5; # field number of the Make field
$Type_number = 6; # field number of the Type field

open (DB, "<$db_file_name") or &cgierr("unable to open $db_file_name. Reason: $!");
if ($db_use_flock) { flock(DB, 1); }
LINE: while (<DB> ) {
next if /^#/;
next if /^\s*$/;
$line = $_;
chomp ($line);
@fields = &split_decode ($line);
if (!(grep $_ eq $fields[$Type_number], @{$Types{$fields[$Merk_number]}})) {
push (@{$Types{$fields[$Merk_number]}}, $fields[$Type_number]);
}
if (!(grep $_ eq $fields[$Merk_number], @Merks)) {
push (@Merks, $fields[$Merk_number]);
}
}
close DB;

Then print it out, using

Code:
foreach $Merk (sort @Merks) {
print "$Merk<BR>";
foreach $type (sort @{$Types{$Merk}}) {
print " $type<BR>";
}
print "<BR>";
}

There's no point in going any further, if you don't get the correct printout.


------------------
JPD





Quote Reply
Re: Select field depended of other In reply to
OK, I have the list as before, only without the doubles Smile, looks were on the good way.
I will use this in a seperate frame, so thats why I think its better to have another script running this. That frame stays always on top.
> >