Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

Form Attributes (action, method)


HTML forms are essential for collecting user input, and they often send this data to a server for processing. The action and method attributes play a key role in defining how and where the form data is sent. This article explores these two attributes and demonstrates their usage with examples.

The action Attribute

The action attribute specifies the URL where the form data should be sent when the form is submitted. This URL typically points to a server-side script that processes the data, such as a PHP file or an API endpoint.

Example of action Attribute:

    <form action="submit_form.php">
        <label>Username:</label>
        <input type="text" name="username"> <br>
        <input type="submit" value="Submit">
    </form>
        

In this example, the form data will be sent to submit_form.php when the submit button is clicked. The server will then handle the data as defined in submit_form.php.

The method Attribute

The method attribute defines the HTTP method used to send form data. It has two primary values:

Example of method="GET":

    <form action="search.php" method="get">
        <label>Search Query:</label>
        <input type="text" name="query"> <br>
        <input type="submit" value="Search">
    </form>
        

In this example, the form data is appended to the URL when submitted, resulting in a URL like search.php?query=your-search-term. This is useful for search forms where data visibility is not an issue.

Example of method="POST":

    <form action="login.php" method="post">
        <label>Username:</label>
        <input type="text" name="username"> <br>
    
        <label>Password:</label>
        <input type="password" name="password"> <br>
    
        <input type="submit" value="Login">
    </form>
        

In this example, the form data is sent as a POST request, which is suitable for sensitive data like passwords. Unlike GET, the data is not appended to the URL, making it more secure.

Combining action and method

Here’s an example that combines the action and method attributes in a single form. This form sends a user's contact information to a server.

    <form action="submit_contact.php" method="post">
        <label>Name:</label>
        <input type="text" name="name"> <br>
    
        <label>Email:</label>
        <input type="email" name="email"> <br>
    
        <label>Message:</label> <br>
        <textarea name="message" rows="4" cols="50"></textarea> <br>
    
        <input type="submit" value="Send">
    </form>
        

In this example, the form data is submitted to submit_contact.php via the POST method, which is ideal for transmitting longer or sensitive information.

Conclusion

The action and method attributes are essential for defining how form data is submitted in HTML. The action attribute specifies the destination of the data, while the method attribute determines the HTTP method used. Choosing the appropriate values for these attributes can enhance both the functionality and security of your forms.



Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java