Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 131
  • Last Modified:

continuation of perl+forms question

I have the following script (from previous question) and I want to modify it a little.  Eventually maybe I'll be able to do this myself...but for now I need someone to rewrite it for me.  The following script reads data passed by a form and spits the information back (in form format) and asks the user if it's correct...then the user submits the data again for the final time or starts over from scratch:

#!/usr/bin/perl

use CGI;

$q=new CGI;

## Send the proper MIME header to the browser.
print "Content-type: text/html\n\n";

if ($q->param){
  ## Read the HTML Form variables into local PERL variables.
  ## NOTE: the variable names are case sensitive. so fn is NOT the same as FN o
r Fn or fN.
  $page=$q->param('page');
  $first_name=$q->param('fn');
  $city=$q->param('city');

  ##  Find out which button the user clicked!!
  $action=$q->param('action');

  if ($page=~ /^1$/){
    print "<B> Are you sure all the info is correct?</B><P>\n";
    $page++;
  }elsif ($page=~ /^2$/){
    if ($action=~ /^Finish$/){  ##  Finish button was clicked
      print "OK!! All your info has been accepted!!<P>\n";
      ## Print the users info!!
      print "First name is <B>$first_name</B><br>\n";
      print "City is <B>$city</B><br>\n";
      exit;
    }elsif ($action=~ /^Start over$/){  ##  Start over button clicked!!
      $page=1;
      $first_name="";
      $city="";
    }
  }
}else{
  $page=1 if (!$page);
}

print qq{
<B>The Basic HTML FORM</B><P>
<FORM METHOD=POST ACTION="$ENV{'SCRIPT_NAME'}">
<B>Enter name </B><input type=text name=fn value=$first_name><BR>
<B>Enter city </B><input type=text name=city value=$city><P>
<input type=hidden name=page value=$page>
};

if ($page=~ /^1$/){
  print qq{
  <input type=submit name=action value="Submit">
  };
}elsif ($page=~ /^2$/){
  print qq{
  <input type=submit name=action value="Finish">
  <input type=submit name=action value="Start over">
  };
}

print qq{
</FORM>
};

exit;

======================================

What I would like to change is this:

1) I want to do away completely with the "start over" button, I'll simply give the user instructions to hit the back button on his browser to change info in the fields. The "start over" option is too cumbersome.

2)  When the user is asked to verify the fields, I don't want the form to just be repeated to the user with the fields filled with the variables.  I would like to have formatted text with the variables inserted so has a professional look.  At the bottom of the page then, there would be the "is this info correct?" text and a submit button to finalize the submition.  Basically, after the user verifies the data, it will be e-mailed to a specified address (I don't want to know how to do this yet...one step at a time).

Please include any comments in the script that will help me understand what's going on and maybe someday I'll understand this stuff.

Thank you!

0
GorGor1
Asked:
GorGor1
  • 3
  • 2
1 Solution
 
maneshrCommented:
here is the script that does both the above things for you.

for point 1 i have commented out the unwanted lines of the code, both PERL & HTML.

for point 2 i have shown the info in a HTML table. you can format the output and make it look as you want it by modifying the HTML tags. Also i have moved the confirmation Q to the bottom of the table.

Finally i have added comments to the code and also put the comment in the code indicating where the email piece is to be inserted.



==============simple.pl
#!/usr/bin/perl

use CGI;

$q=new CGI;

## Send the proper MIME header to the browser.
print "Content-type: text/html\n\n";

if ($q->param){
  ## Read the HTML Form variables into local PERL variables.
  ## NOTE: the variable names are case sensitive. so fn is NOT the same as FN o
r Fn or fN.
  $page=$q->param('page');
  $first_name=$q->param('fn');
  $city=$q->param('city');

  ##  Find out which button the user clicked!!
  $action=$q->param('action');

  if ($page=~ /^1$/){
    #print "<B> Are you sure all the info is correct?</B><P>\n";
    $page++;
  }elsif ($page=~ /^2$/){
    if ($action=~ /^Finish$/){  ##  Finish button was clicked
      print "OK!! All your info has been accepted!!<P>\n";
      ##  The send out as email part of the code will go here.
#      ## Print the users info!!
#      print "First name is <B>$first_name</B><br>\n";
#      print "City is <B>$city</B><br>\n";
      exit;
#    }elsif ($action=~ /^Start over$/){  ##  Start over button clicked!!
#      $page=1;
#      $first_name="";
#      $city="";
    }
  }
}else{
  $page=1 if (!$page);
}

if ($page=~ /^1$/){ ##  Print the First page i.e. HTML form.
  print qq{
  <B>The Basic HTML FORM</B><P>
  <FORM METHOD=POST ACTION="$ENV{'SCRIPT_NAME'}">
  <B>Enter name </B><input type=text name=fn value=$first_name><BR>
  <B>Enter city </B><input type=text name=city value=$city><P>
  <input type=hidden name=page value=$page>
  <input type=submit name=action value="Submit">
  </FORM>
  };
}elsif ($page=~ /^2$/){ ##  Show the user the info entered & seek confirmation.
  print qq{
  <TABLE BORDER=0 CELLSPACING=5>
  <TD><B>Name</B></TD><TD>$first_name</TD>
  <TR>
  <TD><B>City</B></TD><TD>$city</TD>
  <TR>
  <TD COLSPAN=2><B>Is this info correct?</TD>
  <TR>
  <TD COLSPAN=2 ALIGN=CENTER>
  <FORM METHOD=POST ACTION="$ENV{'SCRIPT_NAME'}">
  <input type=hidden name=page value=$page>
  <input type=submit name=action value="Finish">
  <!--input type=submit name=action value="Start over"-->
  </FORM>
  </TD>
  </TABLE>
  };
}

0
 
GorGor1Author Commented:
When I run the script, page1 shows up and asks for the fields to be entered.  When I view the source of that page, it shows up as:

<B>The Basic HTML FORM</B><P>
<FORM METHOD=POST ACTION="/cgi-bin/simple.pl">
<B>Enter name </B><input type=text name=fn value=><BR>
<B>Enter city </B><input type=text name=city value=><P>
<input type=hidden name=page value=1>
<input type=submit name=action value="Submit">
</FORM>

Why is the "value" for name and city blank?  Is this normal?
0
 
maneshrCommented:
".....Why is the "value" for name and city blank?  Is this normal? ...."


yes its normal for the value to be blank, since its the very first page where the user will enter the requried info.

this is a remenant of the "Start over" button.

you can as well eliminate the value= part.

Rgds
0
 
GorGor1Author Commented:
Thank you very much...I will post a continuation question soon.  (Need more tweaking).
0
 
maneshrCommented:
sure. i will try to answer it to the best of my abilities.

Rgds
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now