Form Handling CGI script using C++

Hi,
  Im using C++ code to build a CGI file for operating on form data submitted from a html form.

Form Code : - form.html

<html>
  <head>
    <title>Form Handling CGI Script using C++</title>
  </head>
  <body>
    <center>
      <h2>Form Handling CGI script using C++</h2>
      <form action="cpp_get.cgi" method="get">
First Name: <input type="text" name="first_name">  <br />
Last Name: <input type="text" name="last_name" />
<input type="submit" value="Submit" />
      </form>
    </center>
  </body>
</html>


CGI Script written in C++ : cpp_get.cpp

#include <iostream>
#include <vector>  
#include <string>  
#include <stdio.h>  
#include <stdlib.h> 

#include <cgicc/CgiDefs.h> 
#include <cgicc/Cgicc.h> 
#include <cgicc/HTTPHTMLHeader.h> 
#include <cgicc/HTMLClasses.h>  

using namespace std;
using namespace cgicc;

int main ()
{
   Cgicc formData;
   
   cout << "Content-type:text/html\r\n\r\n";
   cout << "<html>\n";
   cout << "<head>\n";
   cout << "<title>Using GET and POST Methods</title>\n";
   cout << "</head>\n";
   cout << "<body>\n";

   form_iterator fi = formData.getElement("first_name");  
   if( !fi->isEmpty() && fi != (*formData).end()) {  
      cout << "First name: " << **fi << endl;  
   }else{
      cout << "No text entered for first name" << endl;  
   }
   cout << "<br/>\n";
   fi = formData.getElement("last_name");  
   if( !fi->isEmpty() &&fi != (*formData).end()) {  
      cout << "Last name: " << **fi << endl;  
   }else{
      cout << "No text entered for last name" << endl;  
   }
   cout << "<br/>\n";

   cout << "</body>\n";
   cout << "</html>\n";
   
   return 0;


Compile Steps :

$g++ -o cpp_get.cgi cpp_get.cpp -lcgicc

Run in a web server and see the result.

Comments

Unknown said…
Without paramaters you will receive a core dump.
My fix is:

if( fi != (*formData).end() && !fi->isEmpty() ) {
Praneeth Damera said…
hi Rob,

did you get any error kindly paste that error here as when i had worked on this code it had successfully worked.

if you paste that error here i will check it and update it.

i just wanted you to paste error as i don't have its environment ready so if you show me that error i will create its environment and check it and update it.