Create an online survey:The goal is to create a simple online survey and name it - survey.cgi. Allow users to choose at the very least, between two choices (yes or no) on an issue of your choic
Create an online survey:The goal is to create a simple online survey and name it - survey.cgi. Allow users to choose at the very least, between two choices (yes or no) on an issue of your choic
Create an online survey:
- The goal is to create a simple online survey and name it - survey.cgi. Allow users to choose at the very least, between two choices (yes or no) on an issue of your choice. The vote will be saved to the server, and the result of all votes will be displayed on the screen. Also, a vote from the same IP address should not be accepted. The result from the survey should show up every time, even before we fill out the survey. Functions parse and param from retrieve_form.cpp will be reused but modified so that cnt (the count of fields in the form) will not be known at compile time - the program will need to first figure out how many fields are being retrieved from the form (so that the same parse and param functions can be used with any form).
That's it :)
- This is a 'dynamic CGI program' (or self referential), meaning that the code has to respond from two possibilities:
- the script can be called from a link for the first time, in order to take the survey. That state corresponds to the first level 'else' in the pseudocode below.
- the script can be called from the 'submit' button when the user just answered the survey. That state corresponds to the first level 'if' in the pseudocode below.
- Required pseudocode - in more details below
- ***** before main() - global area ******* Constant cnt calls function how_many() to count the number of = signs (number of fields)
- ***** main() ****** call create_array function to create dynamic name_value_pairs array of cnt elements use parse() and param() functions to retrieve the vote from name_value_pairs array retrieve the remote address field from getenv("REMOTE_ADDR") if variable vote is not empty then data is coming from the survey form's submit button: if call to function IP_not_duplicated true then //user has not already voted call save_data function //save the vote to survey.txt call read_data function //populate data_array from survey.txt call display_result function //count and display survey results link to the raw data file survey.txt //display survey.txt itself else call alert_no_vote and exit //user voted already else the module is called from a link, not a submit button call the create_form function //display the survey form call the read_data function //populate data_array from survey.txt call the display_result function //count and display survey results link to the raw data file survey.txt //display survey.txt itself Should include the following functions:
- ***** how_many **************** returns count of = signs in qs - adapt string processing code from function parse()
- ***** create_FIELDS_array(cnt) ** return dynamic pointer to array name_value_pairs of type FIELDS
- ***** parse and param ********** Reuse existing functions to retrieve form vote
- ***** create_form function ****** generate html code to start a form display question and radio buttons for options display submit button end the form Remember to protect double quotes, within a string (already surrounded by quotes). Also use \n characters in the HTML code so that it will print nicely in a 'View Soure' command from the browser. Example (don't use the spaces):
cout << "< html>< head>< title>Survey Vote< /title>< /head>" << "< body>\n" << "< form action=\"survey.cgi\" method=\"GET\">\n" << etc.. "< /html>\n";
- ***** save_data function **** open file survey.txt as type ofstream for appending (ios::app) - more info here: save the vote, concatenated by a pipe symbol with the remote address , as done in the fstream_io 'reformat' function
Example: Y|70.36.224.178 - with a \n at the end to create a new line for the next record
- ***** read_data function ********** open the data file for reading call count_records function into cnt as in fstream_io call create_array to create dynamic array data_array as done before get content of survey.txt into data_array - reuse populate_students_list_array function from fstream_io.cpp
- ***** display_result function ***** tally the choices (as done in random_die_rolls into tally_array) display the result in numbers and bars
- ***** &IP_not_duplicated ********* call the read_data function (if it hasn't been called before) for loop item in data_array split the line into its 2 values (delineated by the pipe symbol) if the address data is equal to the user's REMOTE_ADDR return a zero (the rest of the loop is ignored) end of the if end of for loop return a 1
- Additional Notes:
- Modify the declaration of constant cnt variable to be assigned the value from function how_many(), which determines how many fields are being received from the HTML form. One way to do this is by counting the = signs in QUERY_STRING, using parts of the parse function in a do-while loop, with this type of condition:
do { .. } while (pos != string::npos);
- HTML code to create radio buttons for the yes and no (without the spaces):
< label>Yes< /label>< input type=radio name="vote" value="y">< /input> < label>No< input type=radio name="vote" value="n">< /input>
Note that both of these input tags of type 'radio' are named the same - 'vote'. The field 'vote' will come back with whatever value was selected, or null, if neither were selected.- Example code to get started with buttons: survey_buttons_start Running version
- Modify the param function from the previous program to include only 2 parameters (without the 'int cnt', which is declared globally anyway).
- How to approach this project: SInce this project is rather daunting, the following suggests stages, from which to tackle this work. There are 5 stages, each worth 5 points, which follow the natural progression of what needs to be accomplished. Do not start the next stage until the first one is working correctly. Note: it is ok to turn in a partially completed project. If not complete, simply leave the last stage number (with the label 'stage') on the first line of the browser printout and you will be given credit up to that point. Stages:
- (5 points) Start with a copy of retrieve_form.cpp (since it contains parse and param already) and name it survey.cpp. Create the function how_many() first in order to assign the number of form fields to CONST cnt. Comment unused parts of main() and unused functions definitions and prototypes. Once cnt is known, add the function create_dynamic_array() to create name_value_pairs array. Then uncomment parse() and param() and make these work- just add two fields at the end of the url to test, before the form works (ex: ?first=donald&last=duck).
- (5 points) Once the program retrieves fields, implement function create_form(), which will send to the browser a survey question and retrieve a Yes or No value (at a minimum) from HTML radio buttons. At this point, the program's logic should run the final else in main() if called from a link (not from the form) and run the first 'if' to print debug values coming from param() function (if coming from the form).
- (5 point) The third step is to save the survey data and the remote address to file survey.txt, as shown above, in append mode. At this point don't worry about checking for IP address duplicates. Add this feature only after everything else is working. Hard code a temporary value for the remote address (ra) for the time being, until the last step. During the debug stage of this process, you can open survey.txt in 'pico' or TextWrangler on your machine to check how things are going, delete the data to start over, etc.
- (5 points) Next, create the functions which read the data from file and calculate and display the result - adapt code from random_die_rolls.cpp.
- (5 points) When everything else works, finish by inserting logic which will provide first defense against multiple votes coming from the same IP address, using 'string ra(getenv("REMOTE_ADDR"));'.
- Turn in:
- Create a message in the discussion board providing a workable link to your final html file. Here in review from the assignment HTML and String Processing:
- In a new message for topic named 'retrieve_form.cgi threads', select the check box Enable HTML
- In the body of the message, type the html 'a' tag (shown below) substituting your own url information (and not using a space as it is done here): < a href="http://toolkit.cs.ohlone.edu/~username/web_form.html">Some Descriptive Name for the Project< /a> Make sure there are no return characters in this link code, from < a> to < /a>.
- Note: The system has problems creating the link properly, most of the time (not your fault). If your link does not work, no worries, it will be fixed shortly by the instructor. Just to your best and have the code in place already - thanks :)
- Create a new message for your survey entry - one thread per project, please!
- Take the time to participate in others' surveys!
- Upload survey.cpp to toolkit's dropbox.
- Student Examples:
- Thank you for all of your work - it's been a great semester :)