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_name"};
$dob=$FORM{"dob"};
$birth_country=$FORM{"birth_country"};
$birth_state=$FORM{"birth_state"};
$birth_city=$FORM{"birth_city"};
$contracted_country=$FORM{"contracted_country"};
$contracted_state=$FORM{"contracted_state"};
$contracted_city=$FORM{"contracted_city"};
$current_country=$FORM{"current_country"};
$current_state=$FORM{"current_state"};
$current_city=$FORM{"current_city"};
$gender=$FORM{"gender"};
$sexual_preference=$FORM{"sexual_preference"};
$race=$FORM{"race"};
$marital_status=$FORM{"marital_status"};
$relationship_length_years=$FORM{"relationship_length_years"};
$relationship_length_months=$FORM{"relationship_length_months"};

print "Content-type: text/html\n\n";

print <<end;
<HTML><HEAD><TITLE>Demographics</TITLE></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_name"};
$XXXXX=$FORM{"XXXXX"};

# creates file of form data from previous form
# named 'respondent ID value plus form name'
open(LOGFILE, ">survey_results/$rid-$form_name") || die "Can't open logfile!\n";

# puts values/variables into csv format
print LOGFILE"\"$dob\",\"$birth_country\",\"$birth_state\",\"$birth_city\",\"$contracted_country\",\"$contracted_state\",\"$contracted_city\",\"$current_country\",\"$current_state\",\"$current_city\",\"$gender\",\"$sexual_preference\",\"$race\",\"$marital_status\",\"$relationship_length_years\",\"$relationship_length_months\"\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
morrieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fasterCommented:
I guess there are some problem in the html file (the field name does not match exactly).  add a line here to see what exactly happens:

# puts values/variables into csv format

#the line to be added
print     LOGFILE $message;

print     LOGFILE"\"$dob\",\"$birth_country\", ...
close (LOGFILE);

Then check the log file again and see what is the outcome, if you can not find the problem, post the contents so I can have a look at it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
morrieAuthor Commented:
I added the line you suggested. Here are the pertinent sections from the 2 scripts and the logfile:

# here's the section out of demog.cgi that set up the varibles #
$rid=$FORM{"respondent_id"};
$form_name1=$FORM{"form_name"};
$dob=$FORM{"dob"};
$birth_country=$FORM{"birth_country"};
$birth_state=$FORM{"birth_state"};
$birth_city=$FORM{"birth_city"};
$contracted_country=$FORM{"contracted_country"};
$contracted_state=$FORM{"contracted_state"};
$contracted_city=$FORM{"contracted_city"};
$current_country=$FORM{"current_country"};
$current_state=$FORM{"current_state"};
$current_city=$FORM{"current_city"};
$gender=$FORM{"gender"};
$sexual_preference=$FORM{"sexual_preference"};
$race=$FORM{"race"};
$marital_status=$FORM{"marital_status"};
$relationship_length_years=$FORM{"relationship_length_years"};
$relationship_length_months=$FORM{"relationship_length_months"};
######################################

# here's section out of fam_hx.cgi that prints variables to file #
open(LOGFILE, ">survey_results/$rid-$form_name") || die "Can't open logfile!\n";
print LOGFILE $message;
print LOGFILE"\"$respondent_id\",\"$form_name1\",\"$dob\",\"$birth_country\",\"$birth_state\",\"$birth_city\",\"$contracted_country\",\"$contracted_state\",\"$contracted_city\",\"$current_country\",\"$current_state\",\"$current_city\",\"$gender\",\"$sexual_preference\",\"$race\",\"$marital_status\",\"$relationship_length_years\",\"$relationship_length_months\"\n";
close (LOGFILE);
########################################

## here's the results file ##
respondent_id=1846-871224364&form_name=demographics&dob=3%2F21%2F65&birth_country=canada&birth_state=ontario&birth_city=toranto&contracted_country=canada&contracted_state=ontario&contracted_city=toranto&current_country=canada&current_state=ontario&current_city=toranto&gender=male&sexual_preference=heterosexual&race=cauc&marital_status=comm.+relat.&relationship_length_years=5&relationship_length_months=5"","","","","","","","","","","","","","","","","",""
##########################################

They look OK to me ???




0
fasterCommented:
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}\",";
      }

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

morrieAuthor Commented:
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.
0
fasterCommented:
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{"birth_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.


0
morrieAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.