Using C for CGI Programming(2)
2010-09-18 12:16 shuisheng 阅读(178) 评论(0) 收藏 举报At runtime, I need to be able to configure the database connection.Given a filename and an array of character strings for theconfiguration keys, my configuration function populates acorresponding array of configuration values, as shown in Listing 2.Now I can populate a string array with whatever keys I've chosen touse and get the results back in the value array.
The user interface has two parts. As a programmer, I'mconcerned primarily with the input forms and URL strings. Everybodyelse cares how the page around my form looks and takes theform itself for granted. The solution to keep both parties happy is to havethe page exist separately from the form and my program.
Templating libraries abound in PHP and Perl, but there are no commonHTML templating libraries in C. The easiest solution is to includeonly the barest minimum of the output in my C code and keep therest in HTML files that are output at the appropriate time. Afunction that can do this is found in Listing 3.
Before generating output, I need to tell the Web server and thebrowser what I'm sending; cgiHeaderContentType() accomplishes this task. I want a content type of text/html, so I pass that as theargument. The general steps to follow for any page I want to displayare:
- 
cgiHeaderContentType("text/html"); 
- 
html_get(path, pagetop.html); 
- 
Generate the program content. 
- 
html_get(path, pagebottom.html); 
Now that I can generate a page and print a form, I need to be ableto process that form. I need to read both numeric and text elements,so I use a couple of functions from the cgic library:cgiFormStringNoNewlines() and cgiFormInteger(). The cgic libraryimplements the main function and requires that I implementint cgiMain(void). cgiMain() is where I put the bulk of my formprocessing.
To display a single record in my show_event function, I get theevent_no (my primary key) from the CGI eventno parameter.cgiFormInteger() retrieves an integer value and sets a defaultvalue if no CGI parameter is provided.
I also need to get a whole raft of data from the form insave_event. Dates are thorny things to input because they consist ofthree pieces of data: year, month and date. I need both a begin andan end date, which gives me six fields to interpret. I also need toinput the name of the event, begin and end times (which are stringsbecause they might be events themselves, such as sunrise or sunset)and the location. Listing 4 shows how this works in code.
Listing 4 also demonstrates cgiHeaderLocation(), a functionthat redirects the user to a new page. After I've saved the submitted data,I want to show the event listing page. Instead of a literal string, Iuse one of the variables that libcgic provides,cgiScriptName. Using this variable instead of a literal one means theprogram name can be changed without breaking the program.
 
                    
                     
                    
                 
                    
                 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号