I am changing a GET to a POST. Currently I have .jsp?id=a,b,c,d. When changing this to a post I am still sitting the id parameter a,b,c,d . This is not working for me. Can I submit a comma separated list to a post parameter?
You can do it like a select form input:
url?param=value1¶m=value2¶m=value3
Depending on you language and library you should be able to get an array of values for param.
For example with asp.net mvc i do this to get an array of strings:
string[] values = Request.Form.GetValues("param");
Fionn is right. Use
url?param=value1¶m=value2¶m=value3
to set multiple values to a single parameter. To read the values in your Servlet/JSP you can use
String[] values = request.getParameterValues("param");
Set to have your parameter deliver multiple values by naming it as an array. Instead of calling the input "id", call it "id[]"
<form method="post">
<select multiple name="id[]">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit" value="send">
</form>
When processing, remember to extract the array of values sent by the named parameter.
in post request minimum 1 parameter should be there in parameter list. If you are appending parameter to request then it wont work. for that u need to send parameter as hidden field of form. Or try using following code
function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
http_request.onreadystatechange = alertContents;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
Am I wrong or most of the answers are beside the point?
To answer precisely your question, yes, you can submit a comma separated list to a POST parameter. To be honest, I just did a quick try with a PHP script, but I don't see why Java would behave differently. One point with POST requests is precisely that you have much less constraints on syntax (no need to escape = & or such).
So if you explain more in details what "doesn't work", perhaps we can help you more.
GET and POST have two different purposes. From the Wikipedia HTTP entry:
GET
Requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause. See safe methods below.
POST
Submits data to be processed (e.g. from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.
Basically, you should use GET to, well, GET information, and POST for any actions which alter the state of the server, such as adding new records.