ASP Forms
Forms in ASP are used to collect user inputs and send them to the server for processing. ASP allows handling form submissions using the Request object to retrieve data sent through form fields.
Key Topics
Simple Form Handling
Example
<!-- Simple Form Example -->
<html>
<body>
<form method="post" action="form_handler.asp">
<label for="name">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email"><br><br>
<button type="submit">Submit</button>
</form>
<!-- ASP Code to Handle the Form -->
<%
If Request.Form("name") <> "" Then
Response.Write("Name: " & Request.Form("name") & "<br>")
Response.Write("Email: " & Request.Form("email") & "<br>")
End If
%>
</body>
</html>
Explanation: This example demonstrates a simple form with fields for name and email. The submitted data is retrieved using the Request.Form object and displayed on the same page.
Using POST Method
Example
<!-- POST Method Example -->
<html>
<body>
<form method="post" action="process_form.asp">
<label for="username">Username:</label>
<input type="text" id="username" name="username"><br><br>
<button type="submit">Login</button>
</form>
<!-- ASP Code to Handle the POST Request -->
<%
If Request.Form("username") <> "" Then
Response.Write("Welcome, " & Request.Form("username") & "!")
End If
%>
</body>
</html>
Explanation: This example demonstrates using the POST method to securely send user input (username) to the server for processing. The data is retrieved using the Request.Form object.
Using GET Method
Example
<!-- GET Method Example -->
<html>
<body>
<form method="get" action="process_form.asp">
<label for="query">Search:</label>
<input type="text" id="query" name="query"><br><br>
<button type="submit">Search</button>
</form>
<!-- ASP Code to Handle the GET Request -->
<%
If Request.QueryString("query") <> "" Then
Response.Write("You searched for: " & Request.QueryString("query"))
End If
%>
</body>
</html>
Explanation: This example demonstrates using the GET method to pass search queries through the URL. The data is retrieved using the Request.QueryString object.
Key Takeaways
- Use
POSTfor secure data submission andGETfor passing data via URL. - ASP's
Request.FormandRequest.QueryStringobjects retrieve form data efficiently. - Form handling is essential for creating interactive and user-friendly web applications.