Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • Last Modified:

Test for value of an array?

Hi All,

I have a cgi form that has several question I want filled out. Question 2 has multpile options and an 'other' text box. These options are listed as checkboxes. Users should can fill out as many as apply but they must at least fill out one checkbox or something in the 'other' input box.

<input type=checkbox name=q2_a value=myvalueA>
<input type=checkbox name=q2_b value=myvalueA>
<input type=checkbox name=q2_c value=myvalueA>
<input type=checkbox name=q2_d value=myvalueA>
<input type=text name=q2_other>

The data processing part of the script is grabs the params in the post and checks to see if there is any values passed from those params like so:

my $q2_a      = param('q2_a');
my $q2_b      = param('q2_b');
my $q2_c      = param('q2_c');
my $q2_d      = param('q2_d');
my $q2_other      = param('q2_other')

I load the variables into an array and do the test figuring that if there is no value in the array then the user must not have answered the question and then send them back.

my @q2test = ($q2_a, $q2_b, $q2_c, $q2_d, $q2_other);

if (@q2test eq "") {
            GetTemplate();
            print "You have left a required field blank. Please select an option from question    2.<br>";
            print "Please use the back button and try again. Thank You.";
            print"<p>Debug ~ Q2 Test: @q2test";
            EndTemplate();
      }
It's weird because if I test it and leave all the checkboxes empty and input box it does not throw the error even though the value of the array is nothing.  If I check something or fill out the text box then it passes fine but that doesn't get my goal accomplished.

Anyone have any thoughts? Is there a better way?  

All I want to do is make sure something is answered here. Kinda stumped and any help would be apreciated.

thanks!!
-mike

0
mookie13
Asked:
mookie13
  • 3
1 Solution
 
cwpCommented:
If you use @q2test, it will return the number of elements in the array, so you're really comparing "8" with "".

You can try this instead of the @q2test:
   my $q2 = $q2_a || $q2_b || $q2_c || $q2_d || $q2_other;
   if ($q2 eq "") {
      print "Question 2 field blank\n";
   }

0
 
cwpCommented:
Correction: If you use @q2test *in a scalar context*. @q2test eq "" is scalar enough.
0
 
mookie13Author Commented:
Thank you so much CWP!!! It worked like a charm.
0
 
cwpCommented:
No problem. Not sure if you've noticed, but it's supposed to be 'comparing "5" with ""' as well... I guess 3am isn't a good time for me to answer questions ;).

Anyway, good luck with your program!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now