morrie
asked on
form values to variables & saving to file ?
I want to be able to convert my form results into comma separated values so they can be imported into a MS Access data base. I got some suggestions from someone here at expert exchange and combined that with some code I saw in a script somewhere and came up with something I thought would work. I end up with a file of commas and quotation marks with no data. Can anyone see the mistake here?
SCRIPT 1 - survey1.cgi
#! /usr/bin/perl
print "Content-type: text/html\n\n";
# generates respondent ID ($rid)
$rid = $$ . "-" . time();
print <<end;
<HTML><HEAD><TITLE>Survey Introduction</TITLE></HEAD >
<BODY background="whitebak.gif" bgcolor="#ffffff" text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="demog.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML text deleted*]
<INPUT TYPE="submit" VALUE="START THE SURVEY">
</FORM>
</body>
</html>
end
SCRIPT 2 - demog.cgi
#! /usr/bin/perl
# read previous form data
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
# parse data
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$ pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9] )/pack("c" ,hex($1))/ eg;
$FORM{$name} = $value;
}
# assign variables to form values
$rid=$FORM{"respondent_id" };
$form_name=$FORM{"form_nam e"};
$dob=$FORM{"dob"};
$birth_country=$FORM{"birt h_country" };
$birth_state=$FORM{"birth_ state"};
$birth_city=$FORM{"birth_c ity"};
$contracted_country=$FORM{ "contracte d_country" };
$contracted_state=$FORM{"c ontracted_ state"};
$contracted_city=$FORM{"co ntracted_c ity"};
$current_country=$FORM{"cu rrent_coun try"};
$current_state=$FORM{"curr ent_state" };
$current_city=$FORM{"curre nt_city"};
$gender=$FORM{"gender"};
$sexual_preference=$FORM{" sexual_pre ference"};
$race=$FORM{"race"};
$marital_status=$FORM{"mar ital_statu s"};
$relationship_length_years =$FORM{"re lationship _length_ye ars"};
$relationship_length_month s=$FORM{"r elationshi p_length_m onths"};
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Demogra phics</TIT LE></HEAD>
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="fam_hx.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
<INPUT TYPE="hidden" NAME="form_name" VALUE="demog">
<h2>Section I. Demographic Information:</h2>
[*HTML deleted*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
SCRIPT 3 - fam_hx.cgi
#! /usr/bin/perl
# read input from previous form
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
# parse form data from previous form
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$ pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9] )/pack("c" ,hex($1))/ eg;
$FORM{$name} = $value;
}
# assign variable names
$rid=$FORM{"respondent_id" };
$form_name=$FORM{"form_nam e"};
$XXXXX=$FORM{"XXXXX"};
# creates file of form data from previous form
# named 'respondent ID value plus form name'
open(LOGFILE, ">survey_results/$rid-$for m_name") || die "Can't open logfile!\n";
# puts values/variables into csv format
print LOGFILE"\"$dob\",\"$birth_ country\", \"$birth_s tate\",\"$ birth_city \",\"$cont racted_cou ntry\",\"$ contracted _state\",\ "$contract ed_city\", \"$current _country\" ,\"$curren t_state\", \"$current _city\",\" $gender\", \"$sexual_ preference \",\"$race \",\"$mari tal_status \",\"$rela tionship_l ength_year s\",\"$rel ationship_ length_mon ths\"\n";
close (LOGFILE);
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Family History</TITLE></HEAD>
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="dx1.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
<INPUT TYPE="hidden" NAME="form_name" VALUE="fam_hx">
[*HTML deleted*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
HERE's THE FILE I END UP WITH:
"","","","","","","","","" ,"","","", "","",""," "
OK, I have a feeling I'm missing some big piece here but I cant figure out what it is. I am VERY inexperienced so please make answers as simple as possible.
Thanks in advance for any help,
Morrie
SCRIPT 1 - survey1.cgi
#! /usr/bin/perl
print "Content-type: text/html\n\n";
# generates respondent ID ($rid)
$rid = $$ . "-" . time();
print <<end;
<HTML><HEAD><TITLE>Survey Introduction</TITLE></HEAD
<BODY background="whitebak.gif" bgcolor="#ffffff" text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="demog.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
[*HTML text deleted*]
<INPUT TYPE="submit" VALUE="START THE SURVEY">
</FORM>
</body>
</html>
end
SCRIPT 2 - demog.cgi
#! /usr/bin/perl
# read previous form data
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
# parse data
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]
$FORM{$name} = $value;
}
# assign variables to form values
$rid=$FORM{"respondent_id"
$form_name=$FORM{"form_nam
$dob=$FORM{"dob"};
$birth_country=$FORM{"birt
$birth_state=$FORM{"birth_
$birth_city=$FORM{"birth_c
$contracted_country=$FORM{
$contracted_state=$FORM{"c
$contracted_city=$FORM{"co
$current_country=$FORM{"cu
$current_state=$FORM{"curr
$current_city=$FORM{"curre
$gender=$FORM{"gender"};
$sexual_preference=$FORM{"
$race=$FORM{"race"};
$marital_status=$FORM{"mar
$relationship_length_years
$relationship_length_month
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Demogra
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="fam_hx.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
<INPUT TYPE="hidden" NAME="form_name" VALUE="demog">
<h2>Section I. Demographic Information:</h2>
[*HTML deleted*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
SCRIPT 3 - fam_hx.cgi
#! /usr/bin/perl
# read input from previous form
read(STDIN, $message, $ENV{'CONTENT_LENGTH'});
# parse form data from previous form
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]
$FORM{$name} = $value;
}
# assign variable names
$rid=$FORM{"respondent_id"
$form_name=$FORM{"form_nam
$XXXXX=$FORM{"XXXXX"};
# creates file of form data from previous form
# named 'respondent ID value plus form name'
open(LOGFILE, ">survey_results/$rid-$for
# puts values/variables into csv format
print LOGFILE"\"$dob\",\"$birth_
close (LOGFILE);
print "Content-type: text/html\n\n";
print <<end;
<HTML><HEAD><TITLE>Family History</TITLE></HEAD>
<BODY background="whitebak.gif" bgcolor="#ffffff"
text="#000080" link="#0064ff" vlink="#00a800" alink="#800000">
<FORM ACTION="dx1.cgi" METHOD=POST>
<INPUT TYPE="hidden" NAME="respondent_id" VALUE="$rid">
<INPUT TYPE="hidden" NAME="form_name" VALUE="fam_hx">
[*HTML deleted*]
<INPUT TYPE="submit" VALUE="Go to the next section">
<INPUT TYPE="reset" VALUE="Reset/Re-enter data">
</FORM>
</body>
</html>
end
HERE's THE FILE I END UP WITH:
"","","","","","","","",""
OK, I have a feeling I'm missing some big piece here but I cant figure out what it is. I am VERY inexperienced so please make answers as simple as possible.
Thanks in advance for any help,
Morrie
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Interesting. It seems to correct. Could you email me the scripts and html files (faster@paicific.net.sg)
Are you sure the error message "can't open file" does not show up? Is the log file name correct? Is the following part really included in the 3rd script?
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$ pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9] )/pack("c" ,hex($1))/ eg;
$FORM{$name} = $value;
}
Maybe you can try this: replace the print line with:
foreach $tmp (keys(%FORM)) {
print LOGFILE "\"$FORM{$tmp}\",";
}
Are you sure the error message "can't open file" does not show up? Is the log file name correct? Is the following part really included in the 3rd script?
@pairs=split(/&/, $message);
foreach $pair (@pairs) {
($name,$value)=split(/=/,$
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9]
$FORM{$name} = $value;
}
Maybe you can try this: replace the print line with:
foreach $tmp (keys(%FORM)) {
print LOGFILE "\"$FORM{$tmp}\",";
}
ASKER
I tried it and it did print all the values in comma separated values,
but not in the required order. It does show that all the values are
there. They need to be in a specific order (any order will do, just so it's always the same order) so that when imported into the data base they will end up in the correct columns. It would be very cool if it would work. I have over 100 other scripts, each one with a unique set of values, to add this routine to and it would be much easier to paste in a couple lines of code to each one instead of typing in all those variables.
but not in the required order. It does show that all the values are
there. They need to be in a specific order (any order will do, just so it's always the same order) so that when imported into the data base they will end up in the correct columns. It would be very cool if it would work. I have over 100 other scripts, each one with a unique set of values, to add this routine to and it would be much easier to paste in a couple lines of code to each one instead of typing in all those variables.
I got your email.
This is actually a very simple problem (but I did not notice that until I see your mail).
The 3rd script does not have the lines to assign the variables (in the 2nd script you have):
$dob=$FORM{"dob"};
$birth_country=$FORM{"birt h_country" };
...
Because they have not been assigned the correct value, you got them all empty. So you just need to paste this part of the code to the 3rd script.
To print them in a loop rahther than hard code it, you can use the code I email to you.
This is actually a very simple problem (but I did not notice that until I see your mail).
The 3rd script does not have the lines to assign the variables (in the 2nd script you have):
$dob=$FORM{"dob"};
$birth_country=$FORM{"birt
...
Because they have not been assigned the correct value, you got them all empty. So you just need to paste this part of the code to the 3rd script.
To print them in a loop rahther than hard code it, you can use the code I email to you.
ASKER
Thanks!
ASKER
# here's the section out of demog.cgi that set up the varibles #
$rid=$FORM{"respondent_id"
$form_name1=$FORM{"form_na
$dob=$FORM{"dob"};
$birth_country=$FORM{"birt
$birth_state=$FORM{"birth_
$birth_city=$FORM{"birth_c
$contracted_country=$FORM{
$contracted_state=$FORM{"c
$contracted_city=$FORM{"co
$current_country=$FORM{"cu
$current_state=$FORM{"curr
$current_city=$FORM{"curre
$gender=$FORM{"gender"};
$sexual_preference=$FORM{"
$race=$FORM{"race"};
$marital_status=$FORM{"mar
$relationship_length_years
$relationship_length_month
##########################
# here's section out of fam_hx.cgi that prints variables to file #
open(LOGFILE, ">survey_results/$rid-$for
print LOGFILE $message;
print LOGFILE"\"$respondent_id\"
close (LOGFILE);
##########################
## here's the results file ##
respondent_id=1846-8712243
##########################
They look OK to me ???