zoukankan      html  css  js  c++  java
  • CGI编程入门GET与POST示例

    GET方法:做一个加法运算,需要接收两个参数

    POST方法:做一个乘法运算,需要接收两个参数

    #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 >ost 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\">Errorarameters 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文件:
    <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>lease 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>lease 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>


  • 相关阅读:
    Anniversary party(树形DP入门)
    Neither shaken nor stirred(DFS理解+vector存图)
    统计单词数
    洛谷---三连击
    Educational Codeforces Round 68 (Rated for Div. 2)---B
    HDU-1201--18岁生日
    HDU-盐水的故事
    Flower(规律+逆向思维)
    The puzzle
    XOR Clique(按位异或)
  • 原文地址:https://www.cnblogs.com/MicroGoogle/p/1701009.html
Copyright © 2011-2022 走看看