CGI.PM and IF statment....

2005-09-10 Thread David Gilden
The if statement below does not seem to work as expected//

Given in the HTML:
select name=message type
option value=0Choose type message/option
option value=CommentComment about a recent performance or recording/option
option value=Booking inquiryConcert or Event Booking inquiry/option
option value=Personal messagePersonal message/option
/select


and in the perl:

#!/usr/local/bin/perl 
use CGI qw/:standard/;
use strict;

#and other stuff...

my $mt = param('message type');

if (length($mt) == 0) {
print redirect(/pages/error.html);
exit;
}

This should only redirect it someone has not made a 'selection' 
what am I missing?

Thanks!

Dave
(kora musician / audiophile / webmaster @ www.coraconnection.com  / Ft. Worth, 
TX, USA)

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: CGI.PM and IF statment....

2005-09-10 Thread David Dorward
On Sat, 2005-09-10 at 01:44 -0500, David Gilden wrote:
 select name=message type
 option value=0Choose type message/option

 my $mt = param('message type');
 if (length($mt) == 0) {

 This should only redirect it someone has not made a 'selection' 
 what am I missing?

If the user truly has not made a selection, the $mt will be undef, and
your script will throw a warning:

if (!defined $mt) {
  # etc
}

However, it is very difficult for the user to fail to make a selection
given a select element. It would usually require them to construct their
own form, or HTTP request by hand. If you want to test if they have
selected the Choose type message option, then you need to check if $mt
contains the data 0.

The length of 0 is 1 (since it is one character long), so (length($mt)
== 0) will always fail.

if (
 (!defined $mt) ||
 ($mt eq '0')
   ) {
  # Etc
}

-- 
David Dorward   http://dorward.me.uk/
Anybody remotely interesting is mad, in some way or another.
 -- The Greatest Show in the Galaxy

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




RE: CGI.PM and IF statment....

2005-09-10 Thread Charles K. Clarkson
David Gilden mailto:[EMAIL PROTECTED] wrote:
: The if statement below does not seem to work as expected//
: 
: Given in the HTML:
: select name=message type

Don't use white space in form field names.

select name=message_type


: and in the perl:
: 
: #!/usr/local/bin/perl
: use CGI qw/:standard/;
: use strict;
: 
: #and other stuff...
: 
: my $mt = param('message type');

my $mt = param('message_type');


I also don't use white space in option values.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response




Re: CGI.PM and IF statment....

2005-09-10 Thread Ovid
--- David Gilden [EMAIL PROTECTED] wrote:

 my $mt = param('message type');
 
 if (length($mt) == 0) {
 print redirect(/pages/error.html);
 exit;
 }
 
 This should only redirect it someone has not made a 'selection' 
 what am I missing?

Unless you send bad HTML or someone's messing with their submit data,
it's very unlikely that someone won't have made a selection.  In that
case, you want to check the truth of the selection value, not the
length.  As previously noted, the length of 0 is 1, but you really
don't care about that.  I would leave the value as 0 or set it to the
empty string and write the condition like this:

  if ($mt) { ... }

Cheers,
Ovid

-- 
If this message is a response to a question on a mailing list, please send
follow up questions to the list.

Web Programming with Perl -- http://users.easystreet.com/ovid/cgi_course/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/ http://learn.perl.org/first-response