HTML:CGI实例--表单GET与POST示例
2013-02-28 10:27
288 查看
GET方法:做一个加法运算,需要接收两个参数
文件get.c如下:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
char a[10],b[10];
printf("Content-Type:text/html\n\n");
printf("<HTML>\n");
printf("<HEAD>\n<TITLE >Get Method</TITLE>\n</HEAD>\n");
printf("<BODY>\n");
printf("<div style=\"font-size:12px\">\n");
data = getenv("QUERY_STRING");
if(sscanf(data,"a=%[^&]&b=%s",a,b)!=2){
printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
}
else{
printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">a + b = %d</DIV>\n",atoi(a)+atoi(b));
}
printf("<HR COLOR=\"blue\" align=\"left\" width=\"100\">");
printf("<input type=\"button\" value=\"Back CGI\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
return 0;
}
POST方法:做一个乘法运算,需要接收两个参数
文件post.c如下:
#include <stdio.h>
#include <stdlib.h>
int main(void){
int len;
char *lenstr,poststr[20];
char m[10],n[10];
printf("Content-Type:text/html\n\n");
printf("<HTML>\n");
printf("<HEAD>\n<TITLE >post Method</TITLE>\n</HEAD>\n");
printf("<BODY>\n");
printf("<div style=\"font-size:12px\">\n");
lenstr=getenv("CONTENT_LENGTH");
if(lenstr == NULL)
printf("<DIV STYLE=\"COLOR:RED\">Error parameters should be entered!</DIV>\n");
else{
len=atoi(lenstr);
fgets(poststr,len+1,stdin);
if(sscanf(poststr,"m=%[^&]&n=%s",m,n)!=2){
printf("<DIV STYLE=\"COLOR:RED\">Error: Parameters are not right!</DIV>\n");
}
else{
printf("<DIV STYLE=\"COLOR:GREEN; font-size:15px;font-weight:bold\">m * n = %d</DIV>\n",atoi(m)*atoi(n));
}
}
printf("<HR COLOR=\"blue\" align=\"left\" width=\"100\">");
printf("<input type=\"button\" value=\"Back CGI\" onclick=\"javascript:window.location='../cgi.html'\">");
printf("</div>\n");
printf("</BODY>\n");
printf("</HTML>\n");
fflush(stdout);
return 0;
}
再附上html测试文件cgi.html:
<html>
<head>
<title>CGI Testing</title>
</head>
<body>
<table width="200" height="180" border="0" style="font-size:12px">
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: GET</div>
<div>please input two number:<div>
<form method="get" action="./cgi-bin/get">
<input type="txt" size="3" name="a">+
<input type="txt" size="3" name="b">=
<input type="submit" value="sum">
</form>
</td></tr>
<tr><td>
<div style="font-weight:bold; font-size:15px">Method: POST</div>
<div>please input two number:<div>
<form method="post" action="./cgi-bin/post">
<input type="txt" size="3" name="m">*
<input type="txt" size="3" name="n">=
<input type="submit" value="resu">
</form>
</td></tr>
<tr><td><inputtype="button" value="Back Home"onclick='javascript:window.location="./index.html"'></td></tr>
</table>
</body>
</html>
相关文章推荐
- CGI实例--表单GET与POST示例
- CGI实例--表单GET与POST示例
- CGI实例--表单GET与POST示例
- CGI实例--表单GET与POST示例
- HTTP协议与HTML表单(再谈GET与POST的区别)
- 一 丶get和post是html中表单的提交方式中的两种方式,其主要区别在于:
- HTML表单提交方法POST和GET的区别
- (转)HTTP协议与HTML表单(再谈GET与POST的区别)
- C语言CGI程序--POST表单/GET表单处理
- HTML表单Form中的get和post方法
- HTML【2】表单提交与服务层的模拟(具体解释get与post提交方式的不同)
- HTTP协议与HTML表单 (包含GET与POST的区别)
- Get 与 Post 【总结】 (实例:从a.html到b.aspx传值)
- HTTP协议与HTML表单(再谈GET与POST的区别)
- HTML表单与PHP,POST、GET
- JSP之表单提交get和post的区别详解及实例
- HTTP协议与HTML表单(再谈GET与POST的区别)
- HTTP协议与HTML表单(再谈GET与POST的区别)
- 基于Django URL传参 FORM表单传数据 get post的用法实例
- HTTP协议与HTML表单(再谈GET与POST的区别)