6

As my first web app I developed a very simple survey. Random questions are being asked from the user anytime the page refreshes. The answer is sent to a cgi script using post to save the answers to the database.

However, when the user presses the submit button it automatically goes to the page which is responsible for processing the data and since it doesn't have any output it is a blank page. Now if the user wants to answer another question they have to press the "back" in the browser and refresh the page so a new question pops up. I don't want this.

I want it in a way that when the users pressed submit, the answers go automatically to the processing script and the page refreshes itself with a new question or at least after processing it redirects to the main survey page with a new question.

  • meta http-equiv="refresh" content="0;url=http://example.com/"> is not an option? – khachik May 25 '11 at 10:28
  • @Khachik:where should I put it? in the header? – Hossein May 25 '11 at 10:29
  • it keeps refreshing after the page loads. is there anyway around it? – Thang Do Oct 14 '15 at 3:12
7

You want to implement this: https://en.wikipedia.org/wiki/Post/Redirect/Get

It's much simpler than it sounds. The CGI script that receives the POST must simply produce the following output:

Status: 303 See other
Location: http://lalala.com/themainpage
| improve this answer | |
  • Where to print those output? – Thang Do Oct 14 '15 at 3:10
  • While this works fine in Chrome on my desktop, on my ipad get a safari error saying "too many redirects"... any ideas? I tried emptying the cache on the ipad.. but no luck. – Alex van Es Sep 5 '17 at 17:43
7

You can also send a HTTP header from your processing script:

Location: /

After you have processed your answer, you would send the above header. I would recommend you append a random number query string. e.g. python example (assuming you're using the python CGI module):

#!/usr/bin/env python
import cgitb
import random
import YourFormProcessor

cgitb.enable() # Will catch tracebacks and errors for you. Comment it out if you no-longer need it.

if __name__ == '__main__':
  YourFormProcessor.Process_Form() # This is your logic to process the form.

  redirectURL = "/?r=%s" % random.randint(0,100000000)

  print 'Content-Type: text/html'
  print 'Location: %s' % redirectURL
  print # HTTP says you have to have a blank line between headers and content
  print '<html>'
  print '  <head>'
  print '    <meta http-equiv="refresh" content="0;url=%s" />' % redirectURL
  print '    <title>You are going to be redirected</title>'
  print '  </head>' 
  print '  <body>'
  print '    Redirecting... <a href="%s">Click here if you are not redirected</a>' % redirectURL
  print '  </body>'
  print '</html>'
| improve this answer | |
  • can u give me a more detailed example, i am completely new.don't know where exactly to put this.thx – Hossein May 25 '11 at 11:27
1
<html> 
  <head> 
    <meta http-equiv="refresh" content="0;url=http://www.example.com" /> 
    <title>You are going to be redirected</title> 
  </head> 
  <body> 
    Redirecting...
  </body> 
</html>

See meta-refresh drawbacks and alternatives here.

| improve this answer | |
  • I did it, but my page keeps refreshing after loading...I want it to be refreshed one time after the form submission. it refreshes before I even press the submit button. – Hossein May 25 '11 at 10:37
  • @Hossain, probably, you redirect to a page, which contains that meta as well. – khachik May 25 '11 at 10:38
  • true, the questions are being asked in the main page. after submission i want this main page refreshes again(causing a new question to pop up) – Hossein May 25 '11 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.