Still Life

A Series of Mental Snapshots

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.

–Steve

About these ads

2 Responses to “Two Forms One Page: Explanation of the HTML Form Action”

  1. Ale said

    Wonderful tip! Thanks for sharing.

  2. Niall said

    Hey,
    Very interesting. I was hoping you could help me in a related manner. I have a web page, that does lots of DB querying, and forms. At the top of the page I want a login form, (but you can always use the website without logging in). What is the best way of doing this?

    At the moment Im trying to have the login form (and related, “You are now logged in”) in the “base.html” file and then putting everything else in a “block content” template. But I keep running into problems with having the 2 forms on the page?????

    Thanks,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: