Forms can be submitted using POST or GET methods and can be application/x-www-form-urlencoded or sent as multipart/form-data. The cgi class automatically detects form submission method and encoding and parses the data into form entries and file entries if the page sent files.
Form entries are name/value pairs and file entries are name/filename/tempfilename/mimetype tuples.
The following web page sends several name/value pairs and a file using the POST method. Note the action attribute of the form tag.
<html> <body> Enter your personal information: <form method="post" action="mycgi.cgi"> Name: <input name="name" type="text" value=""><br> Address: <input name="address" type="text" value=""><br> City: <input name="city" type="text" value=""><br> State: <input name="state" type="text" value=""><br> Zip: <input name="zip" type="text" value=""><br> Phone: <input name="phone" type="text" value=""><br> <input name="submit" type="submit" value="Submit"> </form> </body> </html>
The following web page sends several name/value pairs using the GET method. Note the action attribute of the form tag.
<html> <body> Enter your personal information: <form method="get" action="mycgi.cgi"> Name: <input name="name" type="text" value=""><br> Address: <input name="address" type="text" value=""><br> City: <input name="city" type="text" value=""><br> State: <input name="state" type="text" value=""><br> Zip: <input name="zip" type="text" value=""><br> Phone: <input name="phone" type="text" value=""><br> <input name="submit" type="submit" value="Submit"> </form> </body> </html>
The following web page sends several name/value pairs using an anchor with a get string.
<html> <body> <a href="mycgi.cgi?name=David+Muse&address=1234+My+Street&city=Atlanta&state=Georgia&zip=12345&phone=555-444-3333">Submit Information</a> </body> </html>
The following web page sends a file and several name/value pairs. Note the enctype attribute of the form tag. When a file-input tag is used in a form, the enctype of the form must be "multipart/form-data".
<html> <body> Enter your personal information: <form method="post" action="mycgi.cgi" enctype="multipart/form-data"> Name: <input name="name" type="text" value=""><br> Address: <input name="address" type="text" value=""><br> City: <input name="city" type="text" value=""><br> State: <input name="state" type="text" value=""><br> Zip: <input name="zip" type="text" value=""><br> Phone: <input name="phone" type="text" value=""><br> Upload a File: <input name="file" type="file" value=""<br> <input name="submit" type="submit" value="Submit"> </form> </body> </html>
Using the cgi class, the following program reads the form and file entries passed in from the previous page and writes them back out to the browser.
#include <groundwork/cgi.h>
#include <groundwork/cgimodule.h>
#include <iostream.h>
#include <fstream.h>
#include <unistd.h>
MAIN {
cgi *c=new cgi(APISTRUCT);
// send a standard http header
c->sendHeader();
// get the form entries
char *name=c->formEntry("name");
char *address=c->formEntry("address");
char *city=c->formEntry("city");
char *state=c->formEntry("state");
char *zip=c->formEntry("zip");
char *phone=c->formEntry("phone");
// get the file entry
char *originalfilename=c->fileEntryFilename("file");
char *tempfilename=c->fileEntryTempFilename("file");
char *mimetype=c->fileEntryMimeType("file");
// write the form entries back out to the browser
cout << "The following values were passed in from the previous page:<br>" << endl;
cout << "Name: " << name << "<br>" << endl;
cout << "Address: " << address << "<br>" << endl;
cout << "City: " << city << "<br>" << endl;
cout << "State: " << state << "<br>" << endl;
cout << "Zip: " << zip << "<br>" << endl;
cout << "Phone: " << phone << "<br>" << endl;
cout << "<br><br>" << endl;
// write the file metadata back out to the browser
cout << "The following file was uploaded as well:<br>" << endl;
cout << "<br><br>" << endl;
cout << "Filename: " << originalfilename << "<br>" << endl;
cout << "Mime Type: " << mimetype << "<br>" << endl;
cout << "File Content: <br>" << endl;
cout << "<br><br>" << endl;
// write the file itself back out to the browser
ifstream tempfile(tempfilename);
cout << tempfile.rdbuf() << endl;
// remove the temporary file
unlink(tempfilename);
delete c;
}