As we all know, C is a
general-purpose, block structured, procedural, imperative computer
programming language. With C Language we can also do web development.
Here are the steps to do so.
We can implement this in two ways.
1) Get Method
2) Post Method
Get 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”> </p>
<p align=”center”> </p>
<p align=”center”> </p>
<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=”Get”
action=”/cgi-bin/Welcome_Get.cgi”>
<INPUT id=WelcomeFormContents type=hidden name=WelcomeData>
</form>
<p align=”center”> </p>
</body>
</html>
If you see the form action we have mentioned /cgi/Welcome_Get.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.
How to create this Welcome_Get.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”);
Data=getenv(”QUERY_STRING”);
FullSource=strchr(Data,’=');
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;
}
FullSource +=1 ;
Length=strlen(FullSource);
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.
Data=getenv(”QUERY_STRING”); — In get method, we get
the parameters from forms(inputs) in the query string(URL). This syntax
helps us to retreive the query contents. We assign these values to a
string pointer.
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 string
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 visible to the users. We get data through
URL. - The query length is limited to 255 bytes.
Save it as welcome.cpp. Now we have to convert that to Welocme_Get.cgi.
g++ -o Welcome_Get.cgi welcome.cpp
Now copy this Welcome_Get.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.
