Reputation: 6560
I'm trying to post data to a new window (using window.open) from a popup that was opened with the window.open method. What I'm trying to do is pass the selected option value to the newly opened window and load data using $_POST.
Here's what I tried:
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
window.open('/path/to/page.php');
});
});
and currently page.php has a var_dump of $_POST which returns empty. It also opens the page in a new tab instead of a new window - I think this is to do with giving the open windows unique names?
I'm not sure how to get it so $.post works with posting to the same page and opening it with the sent data - any links or solutions you know of that could work?
Thanks :)
Upvotes: 0
Views: 1911
Reputation: 16436
Modify your script like this:
<script type="text/javascript">
$(document).on('click', '.openForm', function()
{
var select = $(this).data('select'),
val = $('#'+ select).val();
$.post('/path/to/page.php', {id: val}, function(res)
{
console.log(res);
var myWindow = window.open("", "MyWindow", "width=600,height=600");
myWindow.document.write(res);
//window.open('test_json.php',1,'width=600, height=600');
});
});
</script>
1) var_dump of $_POST returns empty. Because your both request $.post('/path/to/page.php')
and window.open('/path/to/page.php');
will be different. 1st treated as post and after completion of it window.open('/path/to/page.php');
will be arise which will be get
request.
2) To open window on same not in new tab you have to pass its width and height as 3rd parameter in window.open()
method.
Upvotes: 3