0

trying to send data to a new window that doesn't originate from a form.

This is what I tried:

window.open('/path/to/file.php', 'bundle', 'height=700, width=500');
$.post('/path/to/file.php', {id: $(this).data('id')}, function(res)
{});

but var_dumping $_POST returns an empty array.

the data as you can see is being passed with $(this) (the trigger is an .on('click')) and I'm not a fan of the idea of having to create a hidden form - surely there must be a way to use $.post (or something else) to just post and open a new window - any ideas? Thanks :)

treyBake
  • 6,440
  • 6
  • 26
  • 57
  • What does `this` refer to? Does it have a `data-id` attribute? – Rory McCrossan Apr 05 '17 at 11:57
  • may be this will helps `http://stackoverflow.com/questions/13657362/open-a-new-popup-window-and-post-data-to-it` (open popup and then submit form from there to required script) or `http://stackoverflow.com/questions/3951768/window-open-and-pass-parameters-by-post-method` (hidden form with target) or you need solution by get method then answer by `D. Pachauri` is good – bharat Apr 05 '17 at 12:09
  • @RoryMcCrossan yes - it has a data-id attr – treyBake Apr 05 '17 at 12:33

2 Answers2

1

Found an answer:

$.post('path/to/post.php', {data: 4}, function(res)
{
    var win = window.open('', 'UNIQUE_WINDOW1', 'width=1472, height=300');
    with(win.document)
    {
        open();
        write(res);
        close();
    }
});

this posts the data to post.php, opens the page using with and then writes the response to the window. No performance penalties either it seems. (note - refreshing these pages won't update, you need to trigger the $.post event to update the page)

treyBake
  • 6,440
  • 6
  • 26
  • 57
0

I think you can try something like this

window.open('/path/to/file.php?data1=variable&data2=varaible2', 'bundle', 'height=700, width=500');
D. Pachauri
  • 244
  • 2
  • 8