#! /usr/bin/python3 # Demo of passing information from one page to another # using the <input type="hidden" HTML tag import cgi, cgitb, random cgitb.enable() print('Content-type: text/html\n') PagesDict = {'Page 1':'input_hidden_1.html', \ 'Page 2':'input_hidden_2.html', \ 'Page 3':'input_hidden_3.html'} Question_1 = '''<p>What was the color of Washington's white horse?<br> <input checked="checked" name="answer" type="radio" value="wrong"> Green <input name="answer" type="radio" value="right">White <input name="answer" type="radio" value="wrong"> Transparent <input name="answer" type="radio" value="wrong"> Plaid</p> ''' Question_2 = '''<p>When was the War of 1812?<br> <input checked="checked" name="answer" type="radio" value="wrong"> Huh? <input name="answer" type="radio" value="right">1813-1812 <input name="answer" type="radio" value="wrong">Never happened<br><br>What is the meaning of life?</p> ''' Question_3 = '''<p>What is the meaning of life?<br/> <input checked="checked" name="answer" type="radio" value="right"> 42 <input name="answer" type="radio" value="wrong"> sens de la vie <input name="answer" type="radio" value="wrong">chocolate cake <input name="answer" type="radio" value="wrong">97 in Intro-CS</p> ''' Question_4 = ''' <p>In an alternate universe, this class would be taught by?<br> <input checked="checked" name="answer" type="radio" value="wrong"> Mr. Brooks, with backwards suspenders <input name="answer" type="radio" value="wrong"> Justin Beaver <input name="answer" type="radio" value="wrong">a gopher <input name="answer" type="radio" value="right">guacamole<br></p> ''' Questions = [Question_1,Question_2,Question_3,Question_4] def main(): form = cgi.FieldStorage() # if there are no inputs, then we were called by the user for the # first time. Go to page 1 with score = 0 and no history if 'goto' not in form: goto_page = 'Page 1' score = 0 history = '' last_answer = '' else: goto_page = form.getvalue('goto') answer = form.getvalue('answer') if answer == 'right': last_answer = 'Your last answer was right' current_score = 1 else: last_answer = 'Your last answer was wrong' current_score = -1 # previous values were planted inside the <input type="hidden"> values score = int(form.getvalue('score')) + current_score history = form.getvalue('history') # get the HTML from the next page filename = PagesDict[goto_page] html = open(filename,'r').read() # change stuff in it # show previous pages html = html.replace('<!--history_so_far-->',history) if history == '': history = goto_page else: history = history+','+goto_page # show last answer if last_answer != '': html = html.replace('<!--last_answer-->', last_answer) # show score so far html = html.replace('<!--score_so_far-->',str(score)) # plant hidden score html = html.replace('<!--the_score-->',str(score)) # place history html = html.replace('<!--the_history-->',history) # random question which = random.randint(0,len(Questions)-1) html = html.replace('<!--random_question-->',Questions[which]) # That's it, print it print (html) main()