tags:

views:

44

answers:

3

how to get form input(POST) with following conditions?

1. not hard code the content length
2. a function with provided param which is the form elements name, finally return the string

i can't find any example on internet~

thanks

A: 

A CGI script written in C reads POST data from stdin. The content length is written by the HTTP server in the CONTENT_LENGTH environment variable and the script can use getenv to read it.

Unless you're doing some kind of homework, there are much easier ways to do CGI scripting these days (python, php, etc).

Reference: The Common Gateway Interface (CGI) Version 1.1

diciu
A: 

In addition to what diciu writes:

You also have to take the Content-Type into account, which can be either application/x-www-form-urlencoded or multipart/form-data.

icanhasserver
A: 

Yes, i'm student, learning c~ don't have idea

void getInput2 () {

char *input;
int len;

len = atoi(getenv("CONTENT_LENGTH"));

fgets(input, len+1, stdin);
printf("%s", input[1]); //error

}

friends
You should alloc memory for your input variable so that fgets can write stuff in it. You should also ALWAYS check the results of the functions you call (getenv, fgets). And finally, I think it's considered bad form to answer your question with a different question. If needed, you can edit the original question and add additional data.
diciu
Don't post comments or amendments to your question as an 'answer'. You can edit your own question, and post comments.
Jonathan Leffler