void NonExButtons() { int voteChoices[4]; int i; int result; int invalid;
char **responses;
fprintf(cgiOut, "Votes (method 1): \n"); result = cgiFormCheckboxMultiple("vote", votes, 4, voteChoices, &invalid); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!
fprintf(cgiOut, "Votes (method 2): \n"); result = cgiFormStringMultiple("vote", &responses); if (result == cgiFormNotFound) { fprintf(cgiOut, "I hate them all!
\n"); } else { int i = 0; fprintf(cgiOut, "My preferred candidates are:\n"); fprintf(cgiOut, "
\n"); while (responses[i]) { fprintf(cgiOut, "
%s\n", responses[i]); i++; } fprintf(cgiOut, "
\n"); }
cgiStringArrayFree(responses); }
参考cgiFormStringMultiple() cgiFormStringMultiple()
char **responses;
cgiFormStringMultiple("vote", &responses);
检查CGI环境变量 将用到的变量 这里, 产生图象
#include "cgic.h" #include "gd.h"
char *colors[] = { "red", "green", "blue" };
#define colorsTotal 3
int cgiMain() { int colorChosen; gdImagePtr im; int r, g, b;
im = gdImageCreate(64, 64); r = gdImageColorAllocate(im, 255, 0, 0); g = gdImageColorAllocate(im, 0, 255, 0); b = gdImageColorAllocate(im, 0, 0, 255);
cgiFormSelectSingle("color", 3, &colorChosen, 0);
switch(colorChosen) { case 0: gdImageFill(im, 32, 32, r); break; case 1: gdImageFill(im, 32, 32, g); break; case 2: gdImageFill(im, 32, 32, b); break; }
cgiHeaderContentType("image/gif");
gdImageGif(im, cgiOut);
gdImageDestroy(im); return 0; }
为调试而捕捉CGI状态
cgic函数参考
cgiFormResultType cgiFormString( char *name, char *result, int max) 用于从输入域中copy字符串。他将域名max-1字节中的字符copy到缓冲区result。若域不存在,则copy一个空串到result缓冲区。在此函数中所有的新行由换行符代表。
cgiFormResultType cgiFormStringNoNewlines( char *name, char *result, int max) 它与cgiFormString函数相似,只是所有的CR和LF都被去掉了。
cgiFormResultType cgiFormStringSpaceNeeded( char *name, int *length) 它返回指向name的字符串的长度,并将长度放入length中。
cgiFormResultType cgiFormSelectSingle( char *name, char **choicesText, int choicesTotal, int *result, int defaultV) 取出复选框(跟在select语句之后的),把选择的名字copy到choicesText,把选择的个数copy到choicesTotal,把当前的选择copy到result。 cgiFormResultType cgiFormSelectMultiple( char *name, char **choicesText, int choicesTotal, int *result, int *invalid) 与cgiFormSelectSingle类似,只指向整型数组的result代表了选择的项。
cgiFormResultType cgiFormCheckboxMultiple( char *name, char **valuesText, int valuesTotal, int *result, int *invalid) 与cgiFormCheckboxSingle类似,但它处理同一名字有多个复选框的情况。name指向复选框的名字;valuesText指向包含有每个复选框中参数的一个数组;valuesTotal指向复选框的总数;result是一个整型数组,每个复选框选中的用1代表,没选中的用0代表。
cgiFormResultType cgiFormRadio( char *name, char **valuesText, int valuesTotal, int *result, int defaultV) 与cgiFormCheckboxMultiple相似,只是这里是单选按钮而不是复选框。
This section provides a reference guide to the various global variables provided by cgic for the programmer to utilize. These variables should always be used in preference to stdin, stdout, and calls to getenv() in order to ensure compatibility with the cgic CGI debugging features. 大多数的变量相当于各种CGI变量,重要的是VGIC的变量不能为空.