-if ( $branchcount == 1 ) {
- my ($tmpbranch) = keys %$branches;
- $branchname = $branches->{$tmpbranch}->{branchname};
+
+my @printkeys = sort keys %$printers;
+if (scalar(@printkeys) == 1 or not $printers->{$printer}) {
+ $printer = $printkeys[0]; # if printer didn't really exist, or there is only 1 anyway, then replace it w/ one that does
+}
+
+my @printerloop;
+foreach ( @printkeys ) {
+ next unless ($_); # skip printer if blank.
+ push @printerloop, {
+ selected => ( $_ eq $printer ),
+ name => $printers->{$_}->{'printername'},
+ value => $_,
+ };
+}
+
+my @recycle_loop;
+foreach ($query->param()) {
+ $_ or next; # disclude blanks
+ $_ eq "branch" and next; # disclude branch
+ $_ eq "printer" and next; # disclude printer
+ $_ eq "oldreferer" and next; # disclude oldreferer
+ push @recycle_loop, {
+ param => $_,
+ value => $query->param($_),
+ };
+}
+
+my $referer = $query->param('oldreferer') || $ENV{HTTP_REFERER};
+$referer =~ /selectbranchprinter\.pl/ and undef $referer; # avoid sending them back to this same page.
+
+if (scalar @updated and not scalar @recycle_loop) {
+ # we updated something, and there were no extra params to POST: quick redirect
+ print $query->redirect($referer || '/cgi-bin/koha/circ/circulation.pl');