We already saw how to develop web application
in C Language using ‘Get Method‘.
Now lets see the other method which most of the applications use to
transfer data.
Post Method:
create test.html as following and put it in apache htdocs
folder.
<html>
<head>
<title>Enter Ur Name</title>
<script type=”text/javascript”>
function FormString()
{
var Name=document.getElementById(’Name’).value;
var Place=document.getElementById(’Place’).value;
var string=Name+”^”+Place+”^”;
//alert(string);
submitform(string);
return true;
}
function submitform(String1)
{
document.getElementById(”WelcomeFormContents”).value = String1;
document.getElementById(”WelcomeForm”).submit();
}
</script>
</head>
<body>
<p align=”center”><font face=”Book Antiqua” size=”6″
color=”#800000″><b>
</b></font></p>
<p align=”center”><font face=”Book Antiqua” size=”6″
color=”#800000″><b>Enter
Ur Name </b></font><input type=”text” name=”Name”
size=”20″ id=”Name”value=”L.J.Arthur Neil”></p>
<p align=”center”><font face=”Book Antiqua” size=”6″
color=”#800000″><b>Enter
Ur Place </b></font><input type=”text” name=”Place”
size=”20″ id=”Place” value=”Tuticorin”></p>
<p align=”center”>
<input type=”Button” value=”Submit” name=”SubmitButton”
onclick=”return FormString()”><input type=”reset” value=”Reset”
name=”B2″></p>
<form id=WelcomeForm method=”POST”
action=”/cgi-bin/Welcome_Post.cgi”>
<INPUT id=WelcomeFormContents type=hidden name=WelcomeFormData>
</form>
</body>
</html>
If you see the form action we have mentioned /cgi/Welcome_Post.cgi. On
clicking the submit button, we form the query(form contents) using
Javascript and then submit the form contents. This process of forming query
is to specify particular format so that the extraction will be easier
at the server side. All the things what we have done is similar to ‘Get
Method’. It differs only in the server coding.
How to create this Welcome_Post.cgi. Here it goes,
Write a C program like the following.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char
*UserName=NULL,*LenStr=NULL,*EndPtr=NULL, *Data=NULL,*Place=NULL,*StartPtr=NULL, *FullSource=NULL, *Delimiter=NULL,**OutBuf=NULL;
int Length=0,OutCount=0,*OutLen=0;
printf(”Content-Type:text/html\n\n”);
LenStr=getenv(”CONTENT_LENGTH”);
Length=atoi(LenStr);
Data=(char *)malloc(Length);
fread(Data,Length,1,stdin);
FullSource=strchr(Data,’=');
FullSource +=1 ;
if(!FullSource)
{
printf(”<b><br><br><br><br><br><center><font
color=\”#800000\” size=\”5\”>Couldn’t Locate \”=\” in the
Source</font></center></b>”);
return 0;
}
printf(”<b><br><br><br><br><center><font
color=\”#800000\” size=\”5\”>Source Data
Received</font></center></b>”);
printf(”<b><br><center><font color=\”#800000\”
size=\”5\”>%s</font></center></b>”,Data);
return 0;
}
Printf(”Content-Type:text/html\n\n”); — This
specifies that the code returns html results.
LenStr=getenv(”CONTENT_LENGTH”); — In Post method,
the form contents are passed to the server using HTTP POST transaction
i.e via stdin(Standard Input). The query string(form inputs) are not
visible to the user. This syntax helps to know the amount of data (form
inputs) coming form the server side. This returns the length of the data
coming in string format. We get this value in “LenStr’ a string
pointer.
Length=atoi(LenStr); — Now we get the length using
string to int (atoi) method.
Data=(char *)malloc(Length); — We know the length of
data coming via post method. To get the value in we have to allot
memory to a string. Because we are going to store the data which are coming
from the user side to this string.
fread(Data,Length,1,stdin); — This file operation
helps us to get the data from users which are coming through post
method.
FullSource=strchr(Data,’='); — In the previous we
retrieved the query contents(URL). This contains the hostname, port and
so on. we need only the form inputs like name , place etc.. To get
those, we have to move the pointer to ‘=’. The contents after ‘=’ specifies
the form inputs.
FullSource +=1 ; — This is to move the pointer from
‘=’.
Now we have got the values (inputs) from users in particular format
which we formed using javascript before submitting the form. Do the server
side action required using those values and return the results to the
user. We use ‘printf’ to return the results. This returns html
‘results’ since we have used ‘
printf(”Content-Type:text/html\n\n”);’.
- You can also specify html codes inside printf to format pages.
(eg. - printf(”<font color=green size=5 >Arthur
Neil</font>”)) - The query string is not visible to the users. We get it through
stdin(Standard Input). - The query length is not limited.
Save it as welcome.cpp. Now we have to convert that to
Welocme_Post.cgi.
g++ -o Welcome_Post.cgi Welcome.cpp
Now copy this Welcome_Post.cgi to cgi-bin in the apache
folder. You are done.
Now open test.html in the browser. enter the information and submit.
You will get the server response.
If you get “Page can’t be displayed”, check the permission of the file.
Make it as ‘chmod 755′. Now it should work.
