The Form object

T

Forms are HTML elements that give life to static pages because they provide an interface through which visitors can interact with controls (form elements).

The Form object is how you can interact with this HTML element in your JavaScript scripts.

1. Methods and properties of the object

The properties of the “Form” object are as follows:

action – contains the “action” attribute in the <form> tag, specifies the URL where the form is sent
elements – the array containing all the elements in <form>
encoding – contains the “enctype” attribute in <form>, specifies a MIME encoding of the form
length – the number of items contained in the form
method – contains the “method” attribute in <form>, defines how the form is sent to the server (get or post)
name – contains the “name” attribute of a <form> tag
target – contains the “target” attribute of the <form> tag, specifies the window where the server should return the information. If not specified, the server displays the results in the window that sent the form.
The “Form” object has the following methods:
handleEvent () – calls the specified event handler
reset () – returns the form elements to the default values
submit () – triggers a “submit” event that sends data to the program specified in the “action” attribute of the <form>

2. Send Forms to the Server

The JavaScript language allows processing of data entered in the form before it is sent for processing to the server’s program (script). You can send data to a form using one of two ways:
1. – By calling the submit () method of the “Form” object.
2. – Click on a Submit button that automatically sends the data in the form to which it belongs.

Many of the properties of the “Form” object handle additional information that is sent to the server via the form. These properties are:
action – Specifies the URL of the program to which the form data will be sent
enctype – specifies a MIME encoding of the form, the default value is “application / x-www-form-urlencode”.
The property “enctype =” text / plain “” is used to send unformatted text. With this, the mail will be easier to read, and the message will show the option you selected. Without specifying this property, the email will attach a file with the selected option.
– method – defines how the form data is shipped. The value “get” or “post” can be used.

For the forms using the get method, a CGI test program called “test-cgi” provided with the server is available in the majority of sites (the location of the “test-cgi” file may be different, ask the server administrator). Use the following syntax:
<form action = “http: // sitename / cgi-bin / test-cgi” method = “get”>

You will receive a page containing, among other things, the values ​​of the items in the form. For beginners and those who are not familiar with PHP scripts, the “post” method is recommended.
For PostScript forms, a CGI test program called “post-query” is provided along with the server (its location may be different, ask the server administrator) for most sites. If you want to check what data the form sends, use the following label:

<form action = “http: // sitename / cgi-bin / post-query” method = “post”>
– When you submit the form, you will receive, among other things, the “name-value” pairs corresponding to the elements in the form. If you want to check the form data before it is sent to the server, you can use the “onSubmit” event. – onSubmit – is used to validate the form before it is sent.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories