Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

form values to variables & saving to file ?

Posted on 1997-08-10
6
Medium Priority
?
184 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

If you get a (Blue Screen of Death), your system writes a small file called a minidump. Your first step is to make certain your computer is setup to record memory dumps. Right click My Computer, choose properties. Click on the advanced tab, an…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

704 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