Sams Teach Yourself HTML, CSS & JavaScript Web Publishing in One Hour a Day

(singke) #1
ptg16476052

Using the <form> Tag 321

12


action specifies the URL to which the form is submitted. Again, remember that for the
form to be submitted successfully, the script must be in the exact location you specify
and must work properly.


If you leave out the action attribute, the form is submitted to the current URL. In other
words, if the form appears on the page http://www.example.com/form.html and you leave
off the action attribute, the form will be submitted to that URL by default. This prob-
ably doesn’t seem very useful, but it is if your form is generated by a program instead
of residing in an HTML file. In that case, the form is submitted back to that program for
processing. One advantage of doing so is that if you move the program on the server, you
don’t have to edit the HTML to point the form at the new location.


Although most forms send their data to scripts, you also can make the action link to
another web page or a mailto link. The latter is formed as follows:



This attaches the form data set to an email, which then is sent to the email address listed
in the action attribute. But be aware that there are many things that can go wrong with
using a mailto link in your forms. It’s better to find a program or CGI script to link to in
the action attribute.



To test your forms, I recommend using the get method and leav-
ing out the action attribute of the form tag as shown earlier in
the lesson. When you submit the form, the values you entered
will appear in the URL for the page so that you can inspect them
and make sure that the results are what you expected.

TIP

The method attribute supports two values: get and post. The method indicates how
the form data should be packaged in the request that’s sent back to the server. The get
method appends the form data to the URL in the request. The form data is separated from
the URL in the request by a question mark and is referred to as the query string. If I have
a text input field named searchstring and enter Orangutans in the field, the resulting
URL would look like the following:


http://www.example.com/search?searchstring=Orangutans


The method attribute is not required; if you leave it out, the get method will be used. The
other method is post. Instead of appending the form data to the URL and sending the
combined URL-data string to the server, post sends the form data to the location speci-
fied by the action attribute in the body of the request. This is not readily visible to the
user, and most forms work best with this method. ▼

Free download pdf