1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
using namespace std;
struct FIELDS
{
string name;
string value;
};
const int cnt = 3; //cnt should be set to the number of fields the html form contains
// Prototypes
void parse(string, FIELDS []);
string param(string, FIELDS [], int);
//main begins
int main()
{
FIELDS name_value_pairs [cnt];
string qs(getenv("QUERY_STRING"));
//string qs("first=fred&last=flint&color=red");
cout << "Content-type:text/html\n\n";
cout << "debug with qs: " << qs << "<p>" << endl;
parse(qs, name_value_pairs);
// debug to show content of name_value_pairs
cout << "debug to show content of name_value_pairs array: " << endl << "<br>";
for (int index = 0; index<cnt; index++) {
cout << "name: " << name_value_pairs[index].name << " ";
cout << "value: " << name_value_pairs[index].value << endl << "<br>";
}
// Three fields data are retrieved from the param function
string first = param("first", name_value_pairs, cnt);
string last = param("last", name_value_pairs, cnt);
string color = param("color", name_value_pairs, cnt);
// code an HTML page, which includes the three fields
// received.
return 0;
}
//*******************************************
//******** Functions begin ******************
//*******************************************
//*******************************************
// parse()
// This will separate the name/value pairs found after the ? in
// the URL and populate the name_value_pairs array of structures
//*******************************************
void parse (string qs, FIELDS f_name_value_pairs [])
{
cout << "debug in parse<br>\n" << endl;
string name, value;
int start_pos = 0, pos;
for (int counter=0; counter < cnt; counter++) {
pos = qs.find("=", start_pos);
name = qs.substr(start_pos, pos - start_pos);
cout << "name: " << name << "<br>" << endl;
start_pos = pos + 1;
pos = qs.find("&", start_pos);
if (pos == string::npos) {
pos = qs.length();
}
value = qs.substr(start_pos, pos - start_pos);
start_pos = pos + 1;
}
}
//*******************************************
// param()
// This will find the field value based on the
// field name
//*******************************************
string param(string lookUp, FIELDS f_name_value_pairs[], int f_cnt)
{
}
|