View Full Version : CGI Only Submits Text Input for Some
kellyblueyz
08-15-2007, 03:54 PM
Hi All,
I am having some trouble with my CGI forms and I hope someone here has heard of this. This problem is only happening to 2 users out of ... probably 25-30 who have submitted data.
The problem is the form appears to only be submitting the text input data. The user told me they have a problem with it and when I look I see they registered fine (all text input) but the survey they tried to fill out looks to have only captured the text input. This user is using IE (waiting to hear what version) and says it is not displaying properly and red "x"s show up in the form.
The registration calls a different CGI script than the survey.
Any insight or help would be most appreciated.
FYI - this is the website with the forms: www.thedmdatabase.com
Thanks in advance for any help. Have a great day everyone!
Tamar
whooligan
08-15-2007, 04:19 PM
The problem is the form appears to only be submitting the text input data. The user told me they have a problem with it and when I look I see they registered fine (all text input) but the survey they tried to fill out looks to have only captured the text input.
I don't quite understand what you're saying cuz:
The form is submitting the text input
The user registers with the proper text input
When the survey is filled out, only the text input is captured. . . but you've only mentioned the text input thus far & it seems to be doing everything correctly.
This is a complete guess, but I might assume you have some data either stored in a flat-file database have some data emailed & the other stored in a mysql database?
If that's the case - then maybe these 2 users were trying to add ' (apostrophe's) to their text inputs? An unescaped apostrophe will mess up a mysql query.
To escape an apostrophe from a text input before dumping data into a database:
$inputname = s|\'|\\'|g;
This, however, is only a guess, cuz according to what you wrote, I really don't understand the problem.
FishMonger
08-15-2007, 04:22 PM
You'll need to show us the code in question, otherwise any suggestion we make is pure blind guess work.
kellyblueyz
08-15-2007, 05:18 PM
HI,
Thanks so much for the replies. I didn't explain myself well I am sorry. What I mean is that for just these 2 people it seems like I am only successful at capturing data that is:
<input type="text" ...>
But any other type of input, i.e., checkboxes, radio buttons, selects, etc... I am not capturing the data and getting blanks in the data file. I am not using SQL (even tho it would make more sense but I do not know SQL yet) and writing all the data back to text files. No data gets emailed from the form submission.
Whooligan that's interesting about the apostrophe and SQL. I will have to remember that when I switch it over to SQL. However the successful users do have apostrophes in their text answers and no problems with that.
Here is a snippet of the code (If this person is re-displaying their answers, I have the answers capured in the array @page_record which is probably obvious):
my $qnum = 1;
#============== Question No. 1 (page 1, $page_record[1], breed) ==============
print " <tr>\n";
print " <td>$qnum. What breed of dog is this?</td>\n";
print " <td> <select name=\"breed\">\n";
# This prints the breed 'select' item with the appropriate breed pre-selected
my $i = 0;
foreach my $val (@breed_value) {
if ( $page_record[$pgi] eq $val ) {
print "<option value=\"$val\" selected=\"selected\">$breed_options[$i]</option>\n";
} else {
print "<option value=\"$val\">$breed_options[$i]</option>\n";
}
++$i;
}
print " </select>\n";
print " </td>\n";
print " </tr>\n";
++$pgi;
++$qnum;
#============== Question No. 2 (page 1, $page_record[2], dname) ==============
print " <tr><td>$qnum. Dog's Name</td><td><input type=\"text\" size=\"25\" name=\"dname\" value=\"$page_record[$pgi]\"></td></tr>\n";
++$pgi;
++$qnum;
#============== Question No. 3 (page 1, $page_record[3], dsx) ==============
if ( $page_record[$pgi] eq "female" ) {
print " <tr><td colspan=\"2\">$qnum. What is the sex of this dog?<br /><input type=\"radio\" name=\"dsx\" value=\"female\" checked>Female<br />\n";
print " <input type=\"radio\" name=\"dsx\" value=\"male\">Male</td></tr>\n";
} elsif ( $page_record[$pgi] eq "male" ) {
print " <tr><td colspan=\"2\">$qnum. What is the sex of this dog?<br /><input type=\"radio\" name=\"dsx\" value=\"female\">Female<br />\n";
print " <input type=\"radio\" name=\"dsx\" value=\"male\" checked>Male</td></tr>\n";
} else {
print " <tr><td colspan=\"2\">$qnum. What is the sex of this dog?<br /><input type=\"radio\" name=\"dsx\" value=\"female\">Female<br />\n";
print " <input type=\"radio\" name=\"dsx\" value=\"male\">Male</td></tr>\n";
}
++$pgi;
++$qnum;
#============== Question No. 4 (page 1, $page_record[4], sterilized) ==============
print "\n";
print " <tr><td colspan=\"2\">$qnum. Is this dog spayed or neutered?<br />\n";
if ( $page_record[$pgi] eq "yes" ) {
print " <input type=\"radio\" name=\"sterilized\" value=\"yes\" checked>Yes<br />\n";
} else {
print " <input type=\"radio\" name=\"sterilized\" value=\"yes\">Yes<br />\n";
}
if ( $page_record[$pgi] eq "no" ) {
print " <input type=\"radio\" name=\"sterilized\" value=\"no\" checked>No</td></tr>\n";
} else {
print " <input type=\"radio\" name=\"sterilized\" value=\"no\">No</td></tr>\n";
}
++$pgi;
++$qnum;
I hope that is readable. ANd for the 2 people this not working for it is only the dogs name (question #2, which is text input) that gets written back to the data file. And their registration forms worked which is also all type text input. Everything else fails (Is not captured in the data file). Yet for everyone else this works fine.
I know I changed styles between questions on how I handle it if an answer was the one selected. I am not a programmer but trying my best lol.
It seems so strange to me that those 2 people are only able to capture the text input. Thanks for any help.
FishMonger
08-15-2007, 06:37 PM
It seams that you're more concerned with the positioning/indentation of the rendered html code than you are with your Perl code.
You need to learn about the different quoting methods of Perl.
Here's a cleaned up version of question #3.
if ( $page_record[$pgi] eq "female" ) {
print qq(<tr><td colspan="2">$qnum. What is the sex of this dog?<br />),
qq(<input type="radio" name="dsx" value="female" checked>Female<br />\n),
qq(<input type="radio" name="dsx" value="male">Male</td></tr>\n);
}
elsif ( $page_record[$pgi] eq "male" ) {
print qq(<tr><td colspan="2">$qnum. What is the sex of this dog?<br />),
qq(<input type="radio" name="dsx" value="female">Female<br />\n),
qq(<input type="radio" name="dsx" value="male" checked>Male</td></tr>\n);
}
else {
print qq(<tr><td colspan="2">$qnum. What is the sex of this dog?<br />),
qq(<input type="radio" name="dsx" value="female">Female<br />\n),
qq(<input type="radio" name="dsx" value="male">Male</td></tr>\n);
}
Based on your code sample, it doesn't look like you're using the CGI module, at least you're not using its methods for generating html elements.
Here's how that same thing would be done using the CGI module. http://search.cpan.org/~lds/CGI.pm-3.29/CGI.pm
my $sex = 'male'; # hard coded representation of your $page_record[$pgi] var
print $cgi->radio_group( -name => 'dsx',
-values => ['male','female'],
-default => $sex,
-linebreak => 'true',
);
If you clean up your code and use the CGI module, I'm willing to bet that 99% you're problems will be solved and the remaining issues will be much easier to track down.
kellyblueyz
08-15-2007, 07:01 PM
Thanks FishMonger!!! I am going to give that a try. It sure does look a lot better and I bet you are right that with the way I have it, it's much easier for problems to occur (too much junk in the code!)
Thanks a bunch and I will let you know ...
Tamar
FishMonger
08-15-2007, 07:03 PM
It would be much better and easier if you use a hash for your page_record var instead of the array.
kellyblueyz
08-15-2007, 07:45 PM
I agree with you on the hash thing. At that time I had not used a hash before (okay ... they were scary to me lol) but in the CGI I wrote that displays the results I do use hashes (actually a hash of hashes - nothing like jumping right in!) I have considered going back and changing this part of the code to hashes instead of the array and this is probably the time to do so :)
I am having a problem now tho and I think it is becasue of how I have used a package. Hopefully I will explain this well.
The code snippet I copied into my post and that you kindly re-wrote is part of a package. So when I try to use the CGI radio button html element I am getting an error:
Can't call method "radio_group" on an undefined value at SurveyLib/SurveySubs.pm line 1113
I think this is becasue this code is in a module (SurveySubs.pm) and I must not be defining my modules properly. This is what I have at the top of SurveySubs.pm:
package SurveySubs;
require Exporter;
our @ISA = qw("Exporter");
our @EXPORT = qw(display_page1);
our @EXPORT_OK = qw(display_page1);
use CGI qw(:standard);
(I just added the 'use CGI qw(:standard);' to see if it would help but it does not.)
ANd this is what I Have at the top of the CGI script that calls this library module:
use warnings;
use strict;
use CGI::Carp qw/fatalsToBrowser/;
use CGI qw(:standard);
use SurveyLib::SurveySubs qw(:DEFAULT);
Do I need to have the code that uses the CGI html elements directly in the script itself and not call it from a module? Or do I have my 'use' statements messed up?
Thanks for any help you can give!
FishMonger
08-15-2007, 09:55 PM
Creating the module as you've done creates scoping and namespace issues and is an odd if not bad design. Move your display_page() subs to the main script and drop the SurveySubs module. Or, if you want them in a separate file, make it a .pl file and call it in with a do or require statement and only do so if these subroutines are generic enough to be used in multiple scripts.
A better approach for what I think you're trying to accomplish would be to use HTML::Template.
http://search.cpan.org/~samtregar/HTML-Template-2.9/Template.pm
kellyblueyz
08-15-2007, 10:54 PM
Thanks Fishmonger! I am obviously nothing more than a hacker so I can surely accept it is bad design. I am going to look at that HTML::Template module a little more and then figure out if I can use that or move the subs to the main script. It looks like a pretty cool module and seems it would be pretty helpful. Hopefully I can "get it".
Fortunately my goal is to capture data and not making a website portfolio ... or I would really be in trouble :D
I really appreciate your input and help!!! So do the pups with this disease I am collecting data on!
Tamar
kellyblueyz
08-18-2007, 03:28 AM
Okay I looked this up but I still don't get it. What is the difference between doing this
my $q = new CGI;
print $q->radio_group( -name => 'dage',
-values => ['1 year','2 years','3 years','4 years'],
-default => '$page_record[$pgi]',
-linebreak => 'true',
);
and this
print radio_group( -name => 'dage',
-values => ['1 year','2 years','3 years','4 years'],
-default => '$page_record[$pgi]',
-linebreak => 'true',
);
By the way I did a small test page using this method and it worked for one of the people having problems so I find that very promising.
Thanks much for your help!
Tamar
FishMonger
08-18-2007, 03:48 AM
Okay I looked this up but I still don't get it. What is the difference between doing this
my $q = new CGI;
print $q->radio_group( -name => 'dage',
-values => ['1 year','2 years','3 years','4 years'],
-default => '$page_record[$pgi]',
-linebreak => 'true',
);
and this
print radio_group( -name => 'dage',
-values => ['1 year','2 years','3 years','4 years'],
-default => '$page_record[$pgi]',
-linebreak => 'true',
);
By the way I did a small test page using this method and it worked for one of the people having problems so I find that very promising.
Thanks much for your help!
Tamar
Those are essentially the same, but overall the OO interface provides more functionality, namely it allows you to create and use multiple objects. I use the OO interface most of the time, but I've never needed to create multiple objects.
FishMonger
08-18-2007, 03:51 AM
-default => '$page_record[$pgi]',
That's not going to work. Since you're using single quotes, you won't get the needed variable interpolation, so you'll end up passing '$page_record[$pgi]' as a literal string instead of its interpolated value.
kellyblueyz
08-18-2007, 04:07 AM
That helps a lot. And thanks for pointing out the single quote issue. I guess that would not work would it :o
Code can be so picky :rolleyes: lol
Many thanks again!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.