Two Forms One Page: Explanation of the HTML Form Action
Posted by Steve on April 1, 2009
When I started my web application for my design project I knew absolutely nothing about django, html, css… so basically any and all web technologies. Therefore for a very long time I did not really know the purpose of the action in the form declaration ( <form action=’.’>) or what the ‘.’ actually meant.
It is actually very simple, the ‘ action=”.” ‘ denotes where the form is being submitted (what URL is being called). Specifically, the “.” means that that form will be submitted to the current location (same page), for example if your url was http://mysite.com/search the form would be submitted to http://mysite.com/search.
Now that we know what the action does, it can be used to have two forms on the same page submit to different location. I created a page where a user can either login or register. To have the two forms submit properly I have the following actions for the two forms:
Login Form: <form action=’/accounts/login/’>
Registration Form: <form action=’/register/’>
Now when the login form gets submitted the /accounts/login/ is called and when the registration form gets submitted the data is sent to /register/. An important note is that the action gets performed on the specified location BEFORE the last ‘/’. For example if I had , the action would be performed on the home page and not the register page. Therefore it is important to always have the trailing ‘/’ .
Hopefully this saved someone some trouble getting multiple forms setup.
If you need any clarification, or more details, drop me a line and I’d be more than happy to help.