如何用 C 語言取得 HTML 的表單參數 (post 型)

CGI 程式設計

第一個程式

環境變數

get 參數

post 參數

常用函數

參數解碼

範例:檔案編輯

Apache 安裝

訊息

相關網站

參考文獻

最新修改

簡體版

English

程式一

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[], char *envp[])
{
   printf("Content-type: text/html\n\n");
   int length=atoi(getenv("CONTENT_LENGTH"));
   printf("length=%d\n", length);
   char format[20], post[10000];
   sprintf(format, "%%%dc", length); 
   scanf(format, post);
   printf("post=%s", post);
}

HTML 原始碼

<html>
<body>
<form action="/cgi-bin/post.cgi" method="post">
姓名:<input type="text" name="name"><BR/>
EMAIL:<input type="text" name="email"><BR/>
<input type="submit" value="送出">
</form>
</body>
</html>

執行過程

formpost.png

圖一、網頁輸入後進行 post

cgipost.png

圖二、post.cgi 的輸出結果

Facebook

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License