Detect submit button - ASP Scripts

Sometimes it is necessary to detect which button has been pressed to submit the form, for instance, to redirect the user depending on preferences or chosen action.

If you analyze the HTML source code for a form element you first notice that each submit button is simply an input tag with a type attribute set to submit.
Because it's an input, the value itself will be passed along with any other form inputs when the form itself is submitted.
It means you can read the value via Request.Form like any other form element.

The example below shows you how to print a text depending on the button the user pressed to submit the form.
Needless to say, you can do more depending on your needs. For instance, you can redirect the user to a different location or query different tables from a database.


' Detect which submit button the user pressed
' here submit name is 'submit'
if Request.Form("submit") = "fill" then
    Response.Write("You chosen to fill the form.") 
elseif Request.Form("submit") = "read" then 
    Response.Write("You chosen to read the form.") 
    Response.Write("Select one of the following options:") 
end if 

<form action="default.asp" method="post"> 
<input type="submit" name="submit" value="fill"> 
<input type="submit" name="submit" value="read"> 
</form> and are property of Simone Carletti.
Discover my website or visit my Simone Carletti's profile on LinkedIn