Encoding Form Fields for Use With Hyperlinks
[Previous] [Main] [Next]

 
As mentioned in the previous chapter, the htmlCgi::AsHidden() method provides an easy way to store CGI name/value pairs as hidden fields in a form. There are times, however, when forms are not the best choice for passing persistent data between pages (for example, as hyperlinks). In such cases, you can encode data as part of a URL using the htmlCgi::AsURL() method.  
 
Unlike htmlCgi::AsHidden(), htmlCgi::AsURL() does not accept any arguments. Instead, it returns a String object containing all CGI name/value pairs suitably encoded so that they can be used in a hyperlink or other URL. The resulting string should be prefixed with a CGI script or executable name and a "?" character before use.  
 
For example, suppose you'd like to create a hyperlink to an application and pass a few name/value pairs as parameters to it:  
 
 
Example
 
#include <stdio.h>  
#include <stdlib.h>  
#include <dcmicro/htmlpp/htmlpp.h>  
 
int main( void )  
{  
   htmlPage   page ;  
   htmlCgi    server ;  
 
   //  Create a few name/value pairs to pass to  
   //  the CGI application. These could have come  
   //  from a form.  
   server.Value( "name""John P. Smith" ) ;  
   server.Value( "age", 37 ) ;  
 
   //  Note how we insert a "?" between the  
   //  application and the data.  
   htmlHyperLink  h( "/cgi-bin/yourapp?" + server.AsURL() ) ;  
   h << "Click here" ;  
 
   page << h ;  
 
   server << page ;  
   return 0 ;  
}  
 
 
Program Output
 
Content-Type: text/html  
<HTML>  
<HEAD></HEAD>  
<BODY>  
<A HREF="/cgi-bin/yourapp?name=Smith+P.+Smith&age=37">Click here</A>  
</BODY>  
</HTML>  
 



©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.