aspquestions Posted September 4, 2009 Report Share Posted September 4, 2009 (edited) Hello,This is the code from the asp forms section of this tutorial(tutorial about asp). I was wondering if somebody could clarify a few things for me. <html><body><form action="demo_simpleform.asp" method="post">Your name: <input type="text" name="fname" size="20" /><input type="submit" value="Submit" /></form>[color="#006400"]<%dim fnamefname=Request.Form("fname")If fname<>"" Then Response.Write("Hello " & fname & "!<br />") Response.Write("How are you today?")End If%>[/color]</body></html> In the above form.. how is it determined that the submit button causes the postback?What if there are two buttons..how do we determine which one is causing the postbackIs the asp code ONLY called when the form is posted back??? Can someone explain thesequence of events that happen here? I guess I'm just a bit confused about how and why the code between the <%%> is not called when we first see the form (before the user enters the name etc.) like the html?Really appreciate the help! Edited September 4, 2009 by aspquestions Link to comment Share on other sites More sharing options...
boen_robot Posted September 4, 2009 Report Share Posted September 4, 2009 (edited) The ASP code is first executed when you first request the form. Since you haven't submitted anything, the condition fname<>"" doesn't match (it's false), and therefore nothing between "Then" and "End If" gets executed. You therefore see nothing additional on your screen.You then submit the form to the same page, but this time, you're submitting the form's data. The form reappears unconditionally, and this time, the condition is true, and therefore the code within it gets executed, and you see everything.You can place a name on your submit button. When the form is submitted, the data in other submit buttons is not sent. You can therefore check up to see what is the name and/or value of the submitted button, and act accordingly. For example, if we had <input type="submit" name="submitButton" value="Submit 1" /><input type="submit" name="submitButton" value="Submit 2" /> and you press the "Submit 1" button, ASP will be able to see a form field called "submitButton" with "Submit 1" as value. If you pressed the "Submit 2" button, "Submit 2" would be the value in that field. Since ASP can check that, it could also do things differently depending on it. Edited September 4, 2009 by boen_robot Link to comment Share on other sites More sharing options...
aspquestions Posted September 4, 2009 Author Report Share Posted September 4, 2009 The ASP code is first executed when you first request the form. Since you haven't submitted anything, the conditionfname<>"" doesn't match (it's false), and therefore nothing between "Then" and "End If" gets executed. You therefore see nothing additional on your screen.You then submit the form to the same page, but this time, you're submitting the form's data. The form reappears unconditionally, and this time, the condition is true, and therefore the code within it gets executed, and you see everything.You can place a name on your submit button. When the form is submitted, the data in other submit buttons is not sent. You can therefore check up to see what is the name and/or value of the submitted button, and act accordingly. For example, if we had <input type="submit" name="submitButton" value="Submit 1" /><input type="submit" name="submitButton" value="Submit 2" /> and you press the "Submit 1" button, ASP will be able to see a form field called "submitButton" with "Submit 1" as value. If you pressed the "Submit 2" button, "Submit 2" would be the value in that field. Since ASP can check that, it could also do things differently depending on it. Thank you! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now