Solved

form values to variables & saving to file ?

Posted on 1997-08-10
6
173 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 100 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Recently I have been answering a lot of questions like this in IT forums that I frequent. The question posed is usually something along the lines of "We have software X installed and need to uninstall it for reason Y" or some other variant of the sa…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

778 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