Jump to content

Server Side Code - When Is It Called?


aspquestions

Recommended Posts

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!:)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Thank you! :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...