Ok...this is PERL not PHP!! (This is a PHP forum, but this is just to show you how I did it)
Well, Perl is almost identical to PHP except the big difference is that html
is embedded in Perl while PHP is embedded in html. One of the other
differences is arrays are shown with @ and associative arrays are shown as
%, whereas PHP shows everything with $ Example:
$variable
@array
%hash (associative array)
Anyway, this is the code I used for creating a form based on the contents of
a different form. I have stripped out the stuff that doesn't pertain, but
you should get the idea. You'll notice that in my form, I used a combo box
for selecting the number of steps in the procedure, as I didn't feel like
coding error checking if I needed someone to enter a numerical value in a
text field (error checking usually makes any normal code twice as big LOL):
--- Begin Form 1 (html document):---
<form method="POST" name="step1" action="../cgi-bin/ProcessForm.pl">
<table border="1" width="100%">
<caption>
The contents of this form will be used to create another form. The
second form will be used to fill out the steps of the procedure
</caption>
<tr>
<td>Number of steps:</td>
<td>
<select name="steps">
<option value="1" selected>1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit1" value="Next
Step"><input type="reset" value="Reset Form"></td>
</tr>
</table>
</form>
---End of Form 1---
In the Perl script (please keep in mind that I have greatly reduced this
script just to show you what I did with the data from the first form), I
have taken the value from the "steps" form field from form 1 and used the
sub called "form_contents" to generate the html needed to produce the
additional fields in the second form. Look at the example at the end of
this.
--- Begin Form 2 (written with Perl) ---
#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser carpout);
sub get_params()
{
$steps = param("steps");
}
sub create_form
{
print "Content-type: text/html\n\n";
print <<HTML;
<head>
<title>Add a New Procedure - Step 2</title>
</head>
<body>
<h3>Create a New Procedure - Step 2</h3>
<table align="center" border="1" cellpadding="4" cellspacing="0"
width="570">
<tr>
<td colspan="3">Instructions: Fill out the form to match the paper-based
procedure.<form action="ProcessForm_lockoutb.pl" method="post" name="step2"
target="$proc_num"></td>
</tr>
<tr>
HTML
&form_contents($steps); #This calls out the function for creating the form
fields based on the number of steps chosen
print <<HTML;
<tr>
<td colspan="4"><input type="submit" name="submit2"
value="Preview"> <input type="submit" name="submit2" value="Next Step"
onclick="goback();"> <input type="reset"></td>
</tr>
</table>
</form>
</body>
HTML
}
sub form_contents {
my $steps = $steps;
for ($i=1; $i<=$steps; $i++) {
print <<HTML;
<tr align="center">
<td><b>$i</b></td>
<td><textarea name="arSteps" cols="35" rows="10">Enter step
$i</textarea></td>
</tr>
HTML
}
}
&get_params;
&create_form;
---end of Form 2---
EXAMPLE: If the user chose 3 for the number of steps in form 1, this is
what the resulting HTML code would be:
---Being Example---
<head>
<title>Add a New Procedure Step 2</title>
</head>
<body>
<h3>Create a New Procedure - Step 2</h3>
<table align="center" border="1" cellpadding="4" cellspacing="0"
width="570">
<tr>
<td colspan="3">Instructions: Fill out the form to match the paper-based
lockout procedure.<form action="ProcessForm_lockoutb.pl" method="post"
name="step2" target="$proc_num"></td>
</tr>
<tr align="center">
<td><b>1</b></td>
<td><textarea name="arSteps" cols="35" rows="10">Enter step
1</textarea></td>
</tr>
<tr align="center">
<td><b>2</b></td>
<td><textarea name="arSteps" cols="35" rows="10">Enter step
2</textarea></td>
</tr>
<tr align="center">
<td>3</b></td>
<td><textarea name="arSteps" cols="35" rows="10">Enter step
3</textarea></td>
</tr>
<td colspan="2"><input type="submit" name="submit2" value="Next
Step"><input type="reset"></td>
</tr>
</table>
</form>
</body>
---End Example---
Notice that the textareas all have the same name, "arSteps"? That is so
that each form field that is filled out will return an array when the form
is submitted. So, Step 1 will be in $arSteps[0], Step 2 will be in
$arSteps[1], and Step 3 will be in $arSteps[2].
When I process the second form, I just add the different items in the array
to the database individually and associated these steps with the master
procedure.
I hope this helps you out. I am not very good at explaining things on
paper, as I have more practice standing in front of a classroom teaching
rather than writing LOL.
Let me know if I can help you further 🙂
Jon Trelfa