嵌入式CGI开发之旅——6

时间:2020-12-17 21:09:35
【原创】嵌入式CGI开发之旅——6

实验一、单行文本框

对于网页的话我用DreamWeaver来画,呵呵这样方便很多,当然我也会贴出来代码。

单行文本框一般用来接受一些较短的字符串。

1、  创建源文件:

创建一个新的静态网页文件:mycgictest.html,建立一个单行文本输入域:

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset="utf-8"" />

<title>无标题文档</title>

</head>

<body>

<p>Hello To My CGIC Test!

<form id="form1" name="form1" method="post" action="/cgi-bin/mycgictest.cgi">

  <label for="user_name">Your Name </label>

  <input type="text" name="user_name" id="user_name" />

</form>

</body>

</html>

创建mycgictest.c文件:

#include "cgic.h"

#define USER_NAME_MAX_BYTE  51

int cgiMain(){

    char user_name_result[USER_NAME_MAX_BYTE-1];

    int fetch_result;

 

    cgiHeaderContentType("text/html");

    fprintf(cgiOut,"<html><head>/n");

    fprintf(cgiOut,"<title>my cgic test</title>/n");

    fprintf(cgiOut,"</head>/n");

    fprintf(cgiOut,"<body>/n");

 

    fetch_result=cgiFormStringNoNewlines("user_name",user_name_result,USER_NAME_MAX_BYTE);

    if(fetch_result==cgiFormTruncated){

       fprintf(cgiOut,"OH! You have a mars name,it is too long!/n回火星去吧");

    }else if(fetch_result==cgiFormEmpty){

       fprintf(cgiOut,"OH! You have not input you name!");

    }else if(fetch_result==cgiFormSuccess){

       fprintf(cgiOut,"Whelcom   ");

       fprintf(cgiOut,user_name_result);

    }

    fprintf(cgiOut,"</body></html>/n");

    return 0;

}

输出直接用到了cgiOut,当然如果你想直接通过printf打印出来也可以,不过cgic推荐使用统一的cgiOut来输出,这样能保存程序的一致性,而且用cgiOut来输出不会对性能造成任何的影响。获取表单输入域的数据我用了cgiFormStringNoNewlines()函数,因为单行文本不存在换行问题。(还有一个问题,CGIC能够输出中文,但是不能够处理表单输入域中提交的中文)这里可以看到用CGIC库确实是比较方便的,它屏蔽了GETPOST的差别,不管你是用那种方法都能够正确的获得数据。

2、 编译:如果是在windows中,则用VS2008EP版(呵呵因为我用的是这个版本哈)新建一个工程,然后把cgic.hcgic.cmycgictest.c导入到工程中,编译以后就会生成一个可执行文件,将文件后缀名改为.cgi然后拷贝到自己服务器的cgi-bin目录下。

3、  测试:在浏览器中输入127.0.0.1/mycgictest.html然后在页面的文本框中能够输入一个字符串(不能超过50个字符),回车以后能看到“Whelcom 你输入的字符串就成功了。