How can we extract the values in a form when submitting a form?
For example, the following form:
<Form action = "cgi-bin/out. cgi" method = "Post">
<Input type = "text" name = "name"/>
<Input type = "text" name = "Number"/>
<Input type = "Submit" value = "Submit"/>
</Form>
When an out. cgi request is received, extract the values in the input box "name" and input box "number. And it must be valid no matter whether the action in form is get or post.
The following sample code is provided:
Download: Test. c
- # Include <stdio. h>
- # Include "cgic. H"
- # Include <string. h>
- # Include <stdlib. h>
- Int cgimain (){
- Char name [241];
- Char number [2, 241];
- Cgiheadercontenttype ("text/html ");
- Fprintf (cgiout, "<HTML>
- Fprintf (cgiout, "<title> my CGI </title>
- Fprintf (cgiout, "<body> ");
- Cgiformstring ("name", name, 241 );
- Cgiformstring ("Number", number, 241 );
- Fprintf (cgiout, "
- Fprintf (cgiout, "
- Fprintf (cgiout, "</body>/N ");
- Fprintf (cgiout, "
- Return 0;
- }
As shown in the code above, rows 13th and 14th obtain the value of the input box.
Obtains the input parameter values in cgic. cgiformstring is one of the most commonly used functions.
Cgiformstringnonewlines is used to remove line breaks (if you enter characters in a textarea );
Cgiformstringspaceneeded is used to test the length of the input value. Based on this, you can precisely allocate the buffer as needed.