Passing data between forms |
Try online | Examples |
Here we're going to see how to pass data across several forms.
Here are the different techniques we're using in that example:
<INPUT type="hidden" name="firstname" value="Teebo">
<A href="/cgi-bin/aptilis.exe?file=/home/scripts/multi_forms.e.txt&c=1">Try...</A>
Here's the source of the aptilis program:
sub Init
// Using an Init sub is a good idea to centralize
// Initializations, especially if your script is going
// to move between several servers.
_ScriptUrl = "http://localhost/cgi-bin/aptilis.exe" $
_ScriptPath = "f:/projects/aptilis/documentation/targets/local/scripts/multi_forms.e.txt" $
end Init
sub main
Init()
print("Content-type: text/html\n\n")
// In our example all the forms have this part in common
// Note the use of the c variable, though
print("<HTML>\n")
print("<HEAD>\n\t<TITLE>Our form, stage ", int(c)$, "</TITLE>\n</HEAD>\n")
print("<BODY bgcolor=\"#FFFFD0\">\n")
print("<H1>Our form, stage ", int(c)$, "</H1>\n")
print("<FORM action=\"", _ScriptUrl$, "\" method=\"POST\">\n\n")
print("<INPUT type=\"hidden\" name=\"file\" value=\"", _ScriptPath$, "\">\n")
// We use a control variable to know what to do
select c
case 1
// The next stage
print("<INPUT type=\"hidden\" name=\"c\" value=\"2\">\n\n")
// We get their first name
print("Please enter your first name:<BR>\n")
print("<INPUT type=\"text\" name=\"firstname\" size=30><BR>\n")
break
case 2
// The next stage
print("<INPUT type=\"hidden\" name=\"c\" value=\"3\">\n\n")
// This is the BIG SECRET!!
// How we keep the data across forms
// We have 'firstname' from the previous form.
// Double quotes may break the html
firstname = replace(firstname$, "\"", "'") $
print("<INPUT type=\"hidden\" name=\"firstname\" value=\"", firstname$, "\">\n\n")
// We get their first name
print("Please enter your last name:<BR>\n")
print("<INPUT type=\"text\" name=\"lastname\" size=30><BR>\n")
break
case 3
// The next stage
print("<INPUT type=\"hidden\" name=\"c\" value=\"4\">\n\n")
// Keeping the data from previous forms
lastname = replace(lastname$, "\"", "'") $
print("<INPUT type=\"hidden\" name=\"firstname\" value=\"", firstname$, "\">\n")
print("<INPUT type=\"hidden\" name=\"lastname\" value=\"", lastname$, "\">\n\n")
print("Please type in your address:<BR>\n")
print("<TEXTAREA name=\"address\" cols=30 rows=5></TEXTAREA>\n")
break
case 4
// The next stage
print("<INPUT type=\"hidden\" name=\"c\" value=\"5\">\n\n")
// Keeping the data from previous forms
// Here, we must make sure that carriage returns we may have in the address
// won't break our HTML
// get rid of those annoying char 13 coming from PCs
address = replace(address$, "\r", "") $
// and replace new lines by a code we invented, unlikely to appear in an address
address = replace(address$, "\n", "&nl;") $
print("<INPUT type=\"hidden\" name=\"firstname\" value=\"", firstname$, "\">\n")
print("<INPUT type=\"hidden\" name=\"lastname\" value=\"", lastname$, "\">\n")
print("<INPUT type=\"hidden\" name=\"address\" value=\"", address$, "\">\n\n")
// the next (and last) part of our form
print("You are a:<BR>\n")
print("<INPUT type=\"radio\" value=\"Male\" name=\"sex\" checked> Man<BR>\n")
print("<INPUT type=\"radio\" value=\"Female\" name=\"sex\"> Woman<BR>\n")
break
case 5
// And finally...
print("Here are the details you entered:<BR>\n")
if sex = "Male" $
print("Mr ")
else
print("Mrs ")
end if
print("<FONT color=\"#D00000\">", firstname$, "</FONT> ")
print("<FONT color=\"#00D000\">", lastname$, "</FONT><BR>\n")
// We're transforming address into a displayable piece of HTML text
address = replace(address$, "<", "<") $
address = replace(address$, ">", "<") $
address = replace(address$, "&nl;", "<BR>") $
print("<FONT color=\"#0000D0\">", address$, "</FONT><BR>\n")
break
end select
// part 5 is not a form. so we don't need a submit button
if c != 5
print("<P><INPUT type=\"submit\">\n\n")
end if
// but since we're always printing the form header, we need that
// in order to have a clean HTML page
print("</FORM>\n")
print("</BODY>\n</HTML>\n")
end main
Things learnt