LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 05-18-2012, 07:16 AM   #1
kluther
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
keep selected item in dropdownbox


Hello All,

I'm writhing a perl-cgi-script with 3 dropdownboxes which are dependent on each other. When a value is selected in the first box a java-script is called which will manage the value's in the second dropbox. But when I select a value from the second dropbox the value on screen stays the default value. It gives the right value to the 3e dropbox, but it doesn't look nice. Here is my script (if you save it as testdropbox.pl it should work on your system too):
Code:
#!/usr/bin/perl -w

use CGI;
my $cgi = CGI->new();
print $cgi->header('text/html');

my %locations = (
	'ca' => {
		'desc' => 'Canada',
		'cities' => {
			'ca_to' => 'Toronto',
			'ca_vc' => 'Vancouver',
		},
	},
	'us' => {
		'desc' => 'United States',
		'cities' => {
			'us_ny' => 'New York',
			'us_sf' => 'San Francisco',
		},
	},
);

my %streets = (
	'ca_to' => {
		'desc' => 'Toronto',
		'street' => {
			'ca_aa' => 'a',
			'ca_bb' => 'b',
		},
	},
	'ca_vc' => {
		'desc' => 'Vancouver',
		'street' => {
			'ca_cc' => 'c',
			'ca_dd' => 'd',
		},
	},
);

sub print_cty_dropbox {
	my ($cty) = @_;
	print qq|
	<select id="cty" name="cty">
	<option id="--" | . (!$cty?'selected="selected"':'') . qq|>Please choose country</option>|;
	foreach (keys %locations) {
		print qq|<option value="$_" | . (($cty && ($cty eq $_))?'selected="selected"':'') . qq|>${$locations{$_}}{desc}</option>\n|;
	}
	print "</select>\n";
}

sub print_city_dropbox {
	my ($city) = @_;
	return if (!$city);
	print qq|
	<select id="city" name="city">
	<option id="--" | . (!$city?'selected="selected"':'') . qq|>Please choose city</option>|;
	foreach (keys %{$locations{$city}->{'cities'}}) {
		print qq|<option value="$_" | . (($city && ($city eq $_))?'selected="selected"':'') . qq|>$locations{$city}->{'cities'}{$_}</option>\n|;
	}
	print "</select>\n";
}

sub print_street_dropbox {
	my ($street) = @_;
	return if (!$street);
	print qq|
	<select id="street" name="street">
	<option id="--" selected="selected">Please choose street</option>|;
	foreach (keys %{$streets{$street}->{'street'}}) {
		print qq|<option value="$_">| . $streets{$street}->{'street'}{$_} .  qq|</option>\n|;
	}
	print "</select>\n";
}

print qq|
<html>
	<head>
		<title>Dropbox Demo</title>
	</head>
	<body>
		<form action="testdropbox.pl" method="get" id="tst">
		<table><tr><td>
|;
print_cty_dropbox($cgi->param('cty'));
print qq|
		</td><td>
|;
print_city_dropbox($cgi->param('cty'));
print qq|
		</td><td>
|;
print_street_dropbox($cgi->param('city'));
print qq|
		</td></tr></table>
		</form>
		<script type="text/javascript"><!--
			document.getElementById('cty').onchange = function() {
				document.getElementById('tst').submit();
			};
		// --></script>
		<script type="text/javascript"><!--
			document.getElementById('city').onchange = function() {
				document.getElementById('tst').submit();
			};
		// --></script>
	</body>
</html>
|;
Anybody any suggestions?

Thanx Kluther

Last edited by kluther; 05-20-2012 at 04:44 AM.
 
Old 05-18-2012, 11:56 PM   #2
dugan
Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 5,002

Rep: Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558Reputation: 1558
You can format source code by wrapping it in CODE tags. There's a button for that in the toolbar of the advanced editor. That will preserve the indents and make your code sample readable.

Last edited by dugan; 05-18-2012 at 11:57 PM.
 
Old 05-20-2012, 04:45 AM   #3
kluther
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanx Dugan, done that
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GRUB > Error 28: Selected item cannot fit into memory noisebleed Linux - Kernel 6 09-05-2009 09:09 PM
Error 28: Selected item cannot fit into memory Fuzia Solaris / OpenSolaris 1 12-12-2007 06:10 AM
GRUB: Error 28: Selected item cannot fit into memory Tylo Linux - General 15 06-13-2007 07:17 AM
Error 28: Selected item can not fit into memory Randux Solaris / OpenSolaris 11 02-07-2007 03:37 PM
Selected item cannot fit into memory jolly_good2000 Linux - General 3 03-17-2003 11:00 PM


All times are GMT -5. The time now is 05:25 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration