HTML form POST method with querystring in action URL
Asked Answered
U

3

8

Lets say I have a form with method=POST on my page. Now this form has some basic form elements like textbox, checkbox, etc It has action URL as http://example.com/someAction.do?param=value

I do understand that this is actually a contradictory thing to do, but my question is will it work in practice.

So my questions are;

  1. Since the form method is POST and I have a querystring as well in my URL (?param=value) Will it work correctly? i.e. will I be able to retrieve param=value on my receiving page (someAction.do)

  2. Lets say I use Java/JSP to access the values on server side. So what is the way to get the values on server side ? Is the syntax same to access value of param=value as well as for the form elements like textbox/radio button/checkbox, etc ?

Unesco answered 25/4, 2012 at 6:49 Comment(2)
yes, you can access both, POST and GET variables on any page. to get those values, use a print_r($_GET); and print_r($_POST);Covenant
JSP, like most scripting languages, can access the GET and POST variables separately or can access them as part of one single dataset ... there will be different functions for each approach (if I remember correctly, the getParameter() method will access both GET and POST parameters regardless. Google can help you find libraries to access each set of parameters separately.)Nicole
B
1

1) YES, you will have access to POST and GET variables since your request will contain both. So you can use $_GET["param_name"] and $_POST["param_name"] accordingly.

2) Using JSP you can use the following code for both:

<%= request.getParameter("param_name") %>

If you're using EL (JSP Expression Language), you can also get them in the following way:

${param.param_name}

EDIT: if the param_name is present in both the request QueryString and POST data, both of them will be returned as an array of values, the first one being the QueryString.

In such scenarios, getParameter("param_name) would return the first one of them (as explained here), however both of them can be read using the getParameterValues("param_name") method in the following way:

String[] values = request.getParameterValues("param_name"); 

For further info, read here.

Bunny answered 21/12, 2014 at 3:43 Comment(2)
You understand the question and you are able to explain it above. But my question is: which value shall prevail for a key name that is present both in the query string and in the form?Edentate
@ifelsemonkey both of them will be returned: the first one is the one from the query string, and the second one is the one in the POST body. Also notice that getParameter() will only return the first one of them (the query string value would prevail), but you can read both of them using getParameterValues in the following way: String[] lines = request.getParameterValues("name"); for further info, read here. Edited my answer accordingly.Bunny
K
0

Yes. You can retrieve these parameters in your action class. Just you have to make property of same name (param in your case) with there getters and setters.

Sample Code

private String param;

{... getters and setters ...}

when you will do this, the parameters value (passed via URL) will get saved into the getters of that particular property. and through this, you can do whatever you want with that value.

Katelyn answered 15/12, 2015 at 6:15 Comment(0)
S
0

The POST method just hide the submitted form data from the user. He/she can't see what data has been sent to the server, unless a special tool is used.

The GET method allows anybody to see what data it has. You can easily see the data from the URL (ex. By seeing the key-value pairs in the query string).

In other words it is up to you to show the (maybe unimportant) data to the user by using query string in the form action. For example in a data table filter. To keep the current pagination state, you can use domain.com/path.do?page=3 as an action. And you can hide the other data within the form components, like input, textarea, etc.

Both methods can be catched in the server with the same way. For example in Java, by using request.getParameter("page").

Sanbenito answered 10/1, 2016 at 16:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.