Home  »  ArticlesProgrammingTechnology   »   How to use Both GET and POST Methods in one Request

How to use Both GET and POST Methods in one Request

Because you are reading this, it tells me that you have somewhat wondered whether or what will happen should you want to use both GET and POST methods in your service requests.

This is not typically taught in your run-off-the-mill web design course and that could play out as to why this could be a question in your mind whether or not you are new to the web development game.

Well, whenever a request is made from a web browser to a web server, there are a couple of unseen bits of data that are passed from the web browser to the server.

Depending on what you are doing this could be one or a combination of COOKIE, GET, and POST variables. Normally you do not control the passing of cookies as the casual website visitor but you may have more influence over the GET and POST methods based on your actions at the point in time.

As the web developer, you have complete control as to how you handle these variables and create an experience for your users.

When Should you use GET and POST Methods?

GET is usually ideal for passing non-sensitive information back to the server. These could be parameters that control which page or what parts of a page to load.

GET is also ideal for smaller amounts of data. A good cap would be about 1025 characters. It is also good to know that browsers have a limit as to how much data can be sent via GET and POST methods.

POST is usually good for handling lots of data that needs to be sent to the server as well as sensitive information that should not be visible to prying
eyes.

This data includes multi-part data collected through HTML forms such as emails and their attachments, login and registration forms, etc.

How About if you Want to use Both in one Action?

There are no restrictions on combining the methods, this is more of a design issue than a technical one. Here we will show you the solution using a simple HTML form and using JavaScript to process the form.

When creating an HTML form you may set some attributes. These are the ACTION and the METHOD attributes. You must set the METHOD to “POST” to pass the form inputs as POST data.

To use the GET method, you then need to add the GET data directly as a query string in the ACTION parameter and it is as simple as that.

<form action="example.php?var1=get1&var2=get2&var3=get3" method="post">

    <input type="hidden" name="var4" value="post1" />
    <input type="hidden" name="var5" value="post2" />

    <input type="submit" value="Submit" />
</form>

The above is an example of a form that submits data using both the GET and POST methods.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.

Available under:
Articles, Programming, Technology