?
Solved

form values to variables & saving to file ?

Posted on 1997-08-10
6
Medium Priority
?
193 Views
Last Modified: 2013-12-25
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
0
Comment
Question by:morrie
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
faster earned 400 total points
ID: 1829576
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
 

Author Comment

by:morrie
ID: 1829577
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
 
LVL 7

Expert Comment

by:faster
ID: 1829578
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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:morrie
ID: 1829579
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
 
LVL 7

Expert Comment

by:faster
ID: 1829580
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
 

Author Comment

by:morrie
ID: 1829581
Thanks!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In a question here at Experts Exchange, a member was looking for "a little app that would allow sound to be turned OFF and ON by simply clicking on an icon in the system tray". This article shows how to achieve that, as well as providing the same OF…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

600 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question