Retrieving Form Fields
[Previous] [Main] [Next]

 
The htmlCgi constructor accepts any C++ istream-compatible input, though typically it is always cin.  
 
When a form is created, it specifies a transmission method of GET or POST that describes how data will be passed to the server.  
 
From the CGI application perspective, if the POST method is used the supplied input stream will be used to obtain the data. Alternatively, if the GET method is used, the CGI data will be obtained from the server environment and the input stream will be ignored.  
 
htmlCgi automatically adapts to both the GET and POST methods, decoding and parsing data using either method into a name/value list for easy management.  
 
Obtaining form data is very straightforward and easy. It's as simple as passing the name of the form field to the htmlCgi::Value() method, which returns the result as a String object.  
 
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlPage   page ;  
 
   //  Construct an htmlCgi object to receive  
   //  the CGI form data.  
   htmlCgi  server( cin ) ;  
 
   //  Get the form field named "lname"  
   page << server( "lname" ) ;  
 
   server << page ;  
   return 0 ;  
}  
 
 
In cases where you don't know the names of the form fields in advance, you can obtain a list of all form fields and their associated values by treating the htmlCgi object as a CNVList-derived list. Because htmlCgi inherits from CNVList, you can usse the standard Enumerate() and EnumerateName() methods.  
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlPage   page ;  
 
   //  Construct an htmlCgi object to receive  
   //  the CGI form data from standard input.  
   htmlCgi  server( cin ) ;  
 
   //  Output all CGI data from the form  
   String  name ;  
   name = server.EnumerateName( "" ) ;  
   while ( name != "" )  
   {  
      page << htmlBold( name )   
           << " : "  
           << server( name )  //  the value  
           << htmlBreak() ;  
 
      //  loop for next form field  
      name = server.EnumerateName( name ) ;  
   }  
 
   server << page ;  
   return 0 ;  
}  
 


©1998 DC Micro Development. All rights reserved.
No portion of this document may be c opied or reproduced without expressed written consent.
html++ is a trademark of DC Micro Development.