13

I'm setting up a form in ASP classic and it will reload after submission (action self)

But this time it shows results of previous submissions, so how can I check that a POST submission has been made?

Like in PHP:

if($_POST['submit']) {
  show results...
}
Th3Alchemist
  • 1,161
  • 2
  • 13
  • 25

1 Answers1

33

You have several options:

Method 1 - Check the request method:

If Request.ServerVariables("REQUEST_METHOD") = "POST" Then
    'Show Results...
End If

Method 2 - add a hidden field to your form with a value then check if that value has been posted:

If Request.form("HiddenValue") = "1" Then
    'Show Results...
End If

Method 3 - Check if the request.form collection contains items:

If Request.Form.Count > 0 Then
    'Show Results...
End If

Method 4 - Post to a querystring (i.e. set action of <form> to ?post=yes)

If Request.QueryString("post") = "yes" Then
    'Show Results...
End If

Which one to pick?

My preferred option is method 4 – as it’s easily visible in the address bar as to what’s going on – if for some reason I want to avoid presenting this level of detail in the url, I tend to use option 3 as it’s easy to implement, requires no changes on the source forms & is reliable. As for the other two methods:

  • Method 1 – I tend to avoid relying on server variables if I don’t have 100% control over the server – no real justification for that, just a general habit I tend to work with.
  • Method 2 – You could substitute a hidden field for another field that will always contain a value.
Toothbrush
  • 2,080
  • 24
  • 33
HeavenCore
  • 7,533
  • 6
  • 47
  • 62
  • Thats nice, but which would you recommend? How do the different approaches compare? – AnthonyWJones May 21 '12 at 12:29
  • @AnthonyWJones Aye, good point, answer edited to give a bit of clarification. – HeavenCore May 21 '12 at 12:47
  • 6
    FWIW, I would recommend Method 1. Weird FUD over a server you don't control is irrational, if you can't trust the server to do something basic like this then perhaps you should find a different host. The `REQUEST_METHOD` variable is there for the express purpose of detecting which HTTP Method is being used. Methods 2 and 4 require the developer to pollute their code with unnecessary content so IMO should be avoided when alternatives are available. Method 4 is also a pretty good approach but lacks the clarity of purpose that Method 1 does. – AnthonyWJones May 22 '12 at 09:15
  • I think even IIS can get the `REQUEST_METHOD` variable right. – le3th4x0rbot Aug 25 '14 at 18:03