Javascript window.open pass values using POST
I have a javascript function that uses window.open to call another page and returning the result.
Here is the section of my code:
var windowFeatures = "status=0, toolbar=0, location=0, men开发者_如何学Goubar=0, directories=0, resizable=1, scrollbars=1";
window.open ('http://www.example.com/index.php?p=view.map&coords=' + encodeURIComponent(coords), 'JobWindow', windowFeatures);
My problem now is that I am passing too much data for the GET to handle and I need to pass it using the POST method.
How can I convert the code above to open the page using the POST method without implement forms all over the page (the page lists 100's of orders with a list of suppliers - I am trying to map the suppliers)
I used a variation of the above but instead of printing html I built a form and submitted it to the 3rd party url:
var mapForm = document.createElement("form");
mapForm.target = "Map";
mapForm.method = "POST"; // or "post" if appropriate
mapForm.action = "http://www.url.com/map.php";
var mapInput = document.createElement("input");
mapInput.type = "text";
mapInput.name = "addrs";
mapInput.value = data;
mapForm.appendChild(mapInput);
document.body.appendChild(mapForm);
map = window.open("", "Map", "status=0,title=0,height=600,width=800,scrollbars=1");
if (map) {
mapForm.submit();
} else {
alert('You must allow popups for this map to work.');
}
Thank you php-b-grader. I improved the code, it is not necessary to use window.open(), the target is already specified in the form.
// Create a form
var mapForm = document.createElement("form");
mapForm.target = "_blank";
mapForm.method = "POST";
mapForm.action = "abmCatalogs.ftl";
// Create an input
var mapInput = document.createElement("input");
mapInput.type = "text";
mapInput.name = "variable";
mapInput.value = "lalalalala";
// Add the input to the form
mapForm.appendChild(mapInput);
// Add the form to dom
document.body.appendChild(mapForm);
// Just submit
mapForm.submit();
for target options --> w3schools - Target
For what it's worth, here's the previously provided code encapsulated within a function.
openWindowWithPost("http://www.example.com/index.php", {
p: "view.map",
coords: encodeURIComponent(coords)
});
Function definition:
function openWindowWithPost(url, data) {
var form = document.createElement("form");
form.target = "_blank";
form.method = "POST";
form.action = url;
form.style.display = "none";
for (var key in data) {
var input = document.createElement("input");
input.type = "hidden";
input.name = key;
input.value = data[key];
form.appendChild(input);
}
document.body.appendChild(form);
form.submit();
document.body.removeChild(form);
}
thanks php-b-grader !
below the generic function for window.open pass values using POST:
function windowOpenInPost(actionUrl,windowName, windowFeatures, keyParams, valueParams)
{
var mapForm = document.createElement("form");
var milliseconds = new Date().getTime();
windowName = windowName+milliseconds;
mapForm.target = windowName;
mapForm.method = "POST";
mapForm.action = actionUrl;
if (keyParams && valueParams && (keyParams.length == valueParams.length)){
for (var i = 0; i < keyParams.length; i++){
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = keyParams[i];
mapInput.value = valueParams[i];
mapForm.appendChild(mapInput);
}
document.body.appendChild(mapForm);
}
map = window.open('', windowName, windowFeatures);
if (map) {
mapForm.submit();
} else {
alert('You must allow popups for this map to work.');
}}
Even though this question was long time ago, thanks all for the inputs that helping me out a similar problem. I also made a bit modification based on the others' answers here and making multiple inputs/valuables into a Single Object (json); and hope this helps someone.
js:
//example: params={id:'123',name:'foo'};
mapInput.name = "data";
mapInput.value = JSON.stringify(params);
php:
$data=json_decode($_POST['data']);
echo $data->id;
echo $data->name;
The code helped me to fulfill my requirement.
I have made some modifications and using a form I completed this. Here is my code-
Need a 'target' attribute for 'form' -- that's it!
Form
<form id="view_form" name="view_form" method="post" action="view_report.php" target="Map" >
<input type="text" value="<?php echo $sale->myvalue1; ?>" name="my_value1"/>
<input type="text" value="<?php echo $sale->myvalue2; ?>" name="my_value2"/>
<input type="button" id="download" name="download" value="View report" onclick="view_my_report();" />
</form>
JavaScript
function view_my_report() {
var mapForm = document.getElementById("view_form");
map=window.open("","Map","status=0,title=0,height=600,width=800,scrollbars=1");
if (map) {
mapForm.submit();
} else {
alert('You must allow popups for this map to work.');
}
}
Full code is explained showing normal form and form elements.
精彩评论