In this post I will show you how to process HTML forms easily using CGIs in C++. I assume you already have basic knowledge of writing CGIs in C++. If you don’t, go ahead and read Introduction to C++ CGI.
Processing forms is the basic function of any CGI script and the main purpose of CGIs. As you probably know, there are two common ways to send form data back to the web server: “post” and “get.” When form data is sent with the “get” method, it is appended to the URL string of the form submission URL. The “post” method is much like the “get” method, except the data is transmitted via HTTP headers and not via the URL itself. When a form uses “get,” it allows the user to easily bookmark the query created by the form, as the data is transmitted in the URL itself. On the other hand, the “post” method allows you to send much more data and spares the user from seeing the data in the URL.
Getting the “post” and “get” data is relatively easy. To get the data sent by “get” you can just call getenv("QUERY_STRING"), and you will receive a pointer to a null-terminated string containing the “get” data. Reading the “post” data is a bit more complicated. The data needs to be read from the standard input, but the program won’t receive an EOF when it reaches the end of the data. Instead, it should stop reading after reading a specified amount of bytes, which is defined in the environment variable “CONTENT_LENGTH.” So you should read getenv("CONTENT_LENGTH") bytes from the standard input to receive the “post” data.