NetBurner 3.3
Creating a Form Post Hander

You have probably encountered forms many times on the web, especially for ecommerce and feedback forms. The format is typically some number of text fields, check boxes, radio buttons, combo boxes and a submit button. When you click on the submit button, the data from the form is sent to the web server as a HTTP POST. The web server then parses the data and takes appropriate action.

A form is created in HTML by the <form> tag. User input is accomplished using the <input> tag, representing text fields, check boxes, radio buttons, etc. For example, the web page below creates a form with a text field. It also uses the CPPCALL tag feature to populate the text field with a value.

<html>
<title>HTML Post Example</title>
<body>
<img src="images/netburner-logo.gif">
<h1><font face="arial">HTML Form Post Example, First Form</font></h1>
<br>
<table>
<form action="form1" method=post>
<tr>
<td width=40> </td>
<td>Enter data to post: <input type="text" name="textForm1" value="<!--CPPCALL WebTextForm1 -->"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</form>
</table>
</body>
</html>

The processing mechanism is a callback similar to what we did with the GET handler. When a post is sent a number of events will occur and can be processed by your callback function.

eStartingPost Occurs once at the first event of a post
eVariable Occurs once for each variable in the form
eFile Occurs if a file is being posted
eEndOfPost Occurs once at the end of post processing


void form1PostCallBack(int sock, PostEvents event, const char * pName, const char * pValue)
{
// Received a call back with an event, check for event type
switch (event)
{
case eStartingPost: // Called at the beginning of the post before any data is sent
break;
case eVariable: // Called once for each variable in the form
if (strcmp("textForm1", pName) == 0 )
{
strncpy(textForm1, pValue, MAX_BUF_LEN-1);
iprintf("textForm1 set to: \"%s\"\r\n", pValue);
}
break;
//Called back with a file handle if the post had a file
case eFile:
break; //No file type here so we do nothing
// Called back when the post is complete. You should send your response here.
case eEndOfPost:
{
RedirectResponse(sock, "page2.html");
}
break;
} //Switch
}
HtmlPostVariableListCallback postForm1("form1*", form1PostCallBack);

In this example we are copying the value entered in the text field to the variable testForm1.



RedirectResponse
void RedirectResponse(int sock, PCSTR new_page)
Redirect a HTTP request to a different page.
Definition: httpinternal.cpp:462