Javascript window.open使用POST传递值

时间:2022-10-22 15:16:45

I have a javascript function that uses window.open to call another page and returning the result.

我有一个javascript函数,它使用window.open来调用另一个页面并返回结果。

Here is the section of my code:

这是我的代码部分:

var windowFeatures = "status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=1, scrollbars=1";
window.open ('http://www.domain.com/index.php?p=view.map&coords=' + encodeURIComponent(coords), 'JobWindow', windowFeatures);

My problem now is that I am passing to much data for the GET to handle and I need to pass it using the POST method.

我现在的问题是我传递了很多数据供GET处理,我需要使用POST方法传递它。

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)

我如何转换上面的代码,使用POST方法打开页面,而不是整个页面上的实现表单(该页面列出了包含供应商列表的100个订单 - 我正在尝试映射供应商)

6 个解决方案

#1


58  

I used a variation of the above but instead of printing html I built a form and submitted it to the 3rd party url:

我使用了以上的变体,但我没有打印html而是构建了一个表单并将其提交给第三方网址:

    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.');
}

#2


34  

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.

谢谢php-b-grader。我改进了代码,没有必要使用window.open(),目标已在表单中指定。

// 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

目标选项 - > w3schools - 目标

#3


10  

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);
}

#4


5  

thanks php-b-grader !

谢谢php-b-grader!

below the generic function for window.open pass values using POST:

在window.open的泛型函数下面使用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.');
}}

#5


2  

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.

即使这个问题很久以前,感谢所有帮助我解决类似问题的输入。我还根据其他人的答案进行了一些修改,并将多个输入/贵重物品制作成单个对象(json);并希望这有助于某人。

js:

JS:

//example: params={id:'123',name:'foo'};

mapInput.name = "data";
mapInput.value = JSON.stringify(params); 

php:

PHP:

$data=json_decode($_POST['data']); 

echo $data->id;
echo $data->name;

#6


1  

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

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.

解释完整代码显示正常形式和表单元素。

#1


58  

I used a variation of the above but instead of printing html I built a form and submitted it to the 3rd party url:

我使用了以上的变体,但我没有打印html而是构建了一个表单并将其提交给第三方网址:

    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.');
}

#2


34  

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.

谢谢php-b-grader。我改进了代码,没有必要使用window.open(),目标已在表单中指定。

// 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

目标选项 - > w3schools - 目标

#3


10  

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);
}

#4


5  

thanks php-b-grader !

谢谢php-b-grader!

below the generic function for window.open pass values using POST:

在window.open的泛型函数下面使用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.');
}}

#5


2  

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.

即使这个问题很久以前,感谢所有帮助我解决类似问题的输入。我还根据其他人的答案进行了一些修改,并将多个输入/贵重物品制作成单个对象(json);并希望这有助于某人。

js:

JS:

//example: params={id:'123',name:'foo'};

mapInput.name = "data";
mapInput.value = JSON.stringify(params); 

php:

PHP:

$data=json_decode($_POST['data']); 

echo $data->id;
echo $data->name;

#6


1  

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

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.

解释完整代码显示正常形式和表单元素。