LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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, 06: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 03:44 AM.
 
Old 05-18-2012, 10:56 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,242

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
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 10:57 PM.
 
Old 05-20-2012, 03:45 AM   #3
kluther
LQ Newbie
 
Registered: Mar 2012
Posts: 4

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


Reply



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 08:09 PM
Error 28: Selected item cannot fit into memory Fuzia Solaris / OpenSolaris 1 12-12-2007 05:10 AM
GRUB: Error 28: Selected item cannot fit into memory Tylo Linux - General 15 06-13-2007 06:17 AM
Error 28: Selected item can not fit into memory Randux Solaris / OpenSolaris 11 02-07-2007 02:37 PM
Selected item cannot fit into memory jolly_good2000 Linux - General 3 03-17-2003 10:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:07 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
Open Source Consulting | Domain Registration