Javascript window.open pass values using POST

php-b-grader picture php-b-grader · Apr 16, 2011 · Viewed 144.4k times · Source

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, 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.

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)

Answer

php-b-grader picture php-b-grader · Apr 17, 2011

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