With window.open method I open new site with parameters, which I have to pass by post method.I've found solution, but unfortunately it doesn't work. This is my code:
<script type="text/javascript">
function openWindowWithPost(url,name,keys,values)
{
var newWindow = window.open(url, name);
if (!newWindow) return false;
var html = "";
html += "<html><head></head><body><form id='formid' method='post' action='" + url +"'>";
if (keys && values && (keys.length == values.length))
for (var i=0; i < keys.length; i++)
html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";
html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</sc"+"ript></body></html>";
newWindow.document.write(html);
return newWindow;
}
</script>
Next, I create arrays:
<script type="text/javascript">
var values= new Array("value1", "value2", "value3")
var keys= new Array("a","b","c")
</script>
And call function by:
<input id="Button1" type="button" value="Pass values" onclick="openWindowWithPost('test.asp','',keys,values)" />
But, when I click on this button, the site test.asp is empty (of course I try get pass values - Request.Form("b")
).
How could I solve this problem, why I can't get pass values?
This question is related to
javascript
I completely agree with mercenary's answer posted above and created this function for me which works for me. It's not an answer, it's a comment on above post by mercenary
function openWindowWithPostRequest() {
var winName='MyWindow';
var winURL='search.action';
var windowoption='resizable=yes,height=600,width=800,location=0,menubar=0,scrollbars=1';
var params = { 'param1' : '1','param2' :'2'};
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", winURL);
form.setAttribute("target",winName);
for (var i in params) {
if (params.hasOwnProperty(i)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = i;
input.value = params[i];
form.appendChild(input);
}
}
document.body.appendChild(form);
window.open('', winName,windowoption);
form.target = winName;
form.submit();
document.body.removeChild(form);
}
Since you wanted the whole form inside the javascript, instead of writing it in tags, you can do this:
let windowName = 'w_' + Date.now() + Math.floor(Math.random() * 100000).toString();
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", "openData.do");
form.setAttribute("target", windowName);
var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", "message");
hiddenField.setAttribute("value", "val");
form.appendChild(hiddenField);
document.body.appendChild(form);
window.open('', windowName);
form.submit();
I've used this in the past, since we typically use razor syntax for coding
@using (Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { target = "_blank" }))
{
// add hidden and form filed here
}
I wanted to do this in React using plain Js and the fetch polyfill. OP didn't say he specifically wanted to create a form and invoke the submit method on it, so I have done it by posting the form values as json:
examplePostData = {
method: 'POST',
headers: {
'Content-type' : 'application/json',
'Accept' : 'text/html'
},
body: JSON.stringify({
someList: [1,2,3,4],
someProperty: 'something',
someObject: {some: 'object'}
})
}
asyncPostPopup = () => {
//open a new window and set some text until the fetch completes
let win=window.open('about:blank')
writeToWindow(win,'Loading...')
//async load the data into the window
fetch('../postUrl', this.examplePostData)
.then((response) => response.text())
.then((text) => writeToWindow(win,text))
.catch((error) => console.log(error))
}
writeToWindow = (win,text) => {
win.document.open()
win.document.write(text)
win.document.close()
}
I found a better way to pass parameters to the popup window and even to retrieve parameters from it :
In the main page :
var popupwindow;
var sharedObject = {};
function openPopupWindow()
{
// Define the datas you want to pass
sharedObject.var1 =
sharedObject.var2 =
...
// Open the popup window
window.open(URL_OF_POPUP_WINDOW, NAME_OF_POPUP_WINDOW, POPUP_WINDOW_STYLE_PROPERTIES);
if (window.focus) { popupwindow.focus(); }
}
function closePopupWindow()
{
popupwindow.close();
// Retrieve the datas from the popup window
= sharedObject.var1;
= sharedObject.var2;
...
}
In the popup window :
var sharedObject = window.opener.sharedObject;
// function you have to to call to close the popup window
function myclose()
{
//Define the parameters you want to pass to the main calling window
sharedObject.var1 =
sharedObject.var2 =
...
window.opener.closePopupWindow();
}
That's it !
And this is very convenient because:
Have Fun!
I created a function to generate a form, based on url, target and an object as the POST
/GET
data and submit method. It supports nested and mixed types within that object, so it can fully replicate any structure you feed it: PHP automatically parses it and returns it as a nested array.
However, there is a single restriction: the brackets [
and ]
must not be part of any key in the object (like {"this [key] is problematic" : "hello world"}
). If someone knows how to escape it properly, please do tell!
Without further ado, here is the source:
function getForm(url, target, values, method) {_x000D_
function grabValues(x) {_x000D_
var path = [];_x000D_
var depth = 0;_x000D_
var results = [];_x000D_
_x000D_
function iterate(x) {_x000D_
switch (typeof x) {_x000D_
case 'function':_x000D_
case 'undefined':_x000D_
case 'null':_x000D_
break;_x000D_
case 'object':_x000D_
if (Array.isArray(x))_x000D_
for (var i = 0; i < x.length; i++) {_x000D_
path[depth++] = i;_x000D_
iterate(x[i]);_x000D_
}_x000D_
else_x000D_
for (var i in x) {_x000D_
path[depth++] = i;_x000D_
iterate(x[i]);_x000D_
}_x000D_
break;_x000D_
default:_x000D_
results.push({_x000D_
path: path.slice(0),_x000D_
value: x_x000D_
})_x000D_
break;_x000D_
}_x000D_
path.splice(--depth);_x000D_
}_x000D_
iterate(x);_x000D_
return results;_x000D_
}_x000D_
var form = document.createElement("form");_x000D_
form.method = method;_x000D_
form.action = url;_x000D_
form.target = target;_x000D_
_x000D_
var values = grabValues(values);_x000D_
_x000D_
for (var j = 0; j < values.length; j++) {_x000D_
var input = document.createElement("input");_x000D_
input.type = "hidden";_x000D_
input.value = values[j].value;_x000D_
input.name = values[j].path[0];_x000D_
for (var k = 1; k < values[j].path.length; k++) {_x000D_
input.name += "[" + values[j].path[k] + "]";_x000D_
}_x000D_
form.appendChild(input);_x000D_
}_x000D_
return form;_x000D_
}
_x000D_
Usage example:
document.body.onclick = function() {_x000D_
var obj = {_x000D_
"a": [1, 2, [3, 4]],_x000D_
"b": "a",_x000D_
"c": {_x000D_
"x": [1],_x000D_
"y": [2, 3],_x000D_
"z": [{_x000D_
"a": "Hello",_x000D_
"b": "World"_x000D_
}, {_x000D_
"a": "Hallo",_x000D_
"b": "Welt"_x000D_
}]_x000D_
}_x000D_
};_x000D_
_x000D_
var form = getForm("http://example.com", "_blank", obj, "post");_x000D_
_x000D_
document.body.appendChild(form);_x000D_
form.submit();_x000D_
form.parentNode.removeChild(form);_x000D_
}
_x000D_
The default submit Action is Ext.form.action.Submit, which uses an Ajax request to submit the form's values to a configured URL. To enable normal browser submission of an Ext form, use the standardSubmit config option.
Link: http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.Basic-cfg-standardSubmit
solution: put standardSubmit :true in your config. Hope that this will help you :)
You could simply use target="_blank"
on the form.
<form action="action.php" method="post" target="_blank">
<input type="hidden" name="something" value="some value">
</form>
Add hidden inputs in the way you prefer, and then simply submit the form with JS.
Even though I am 3 years late, but to simplify Guffa's example, you don't even need to have the form on the page at all:
$('<form method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something">
...
</form>').submit();
Edited:
$('<form method="post" action="test.asp" target="TheWindow">
<input type="hidden" name="something" value="something">
...
</form>').appendTo('body').submit().remove();
Maybe a helpful tip for someone :)
Source: Stackoverflow.com