0

The current scenario in my app is that I need to make a button call a method (service) and open a new tab at the same time (I'm generating a PDF from results of the mentioned service).

At this moment, I'm doing this like so:

// the method that is called
$scope.open = function(){
    CombineDataFactory.sendData('generatePlan.php');
    $window.open('/generatePlan.php', '_blank');
}


// updated as per a suggestion in comments
CombineDataFactory.sendData('generatePlan.php')
        .then(
                function(data){
                    $window.open('/generatePlan.php', '_blank');
                },
                function(error){
                    alert(error);
                }
            );

The underlying problem is that the second line (tab opening) is considered a new request and does not contain any post data, which I obviously need, otherwise my PDF doesn't get the data to work with.

I have tried so far storing the post into a PHP session but it's not working well, I'm getting old session values.

if( isset($_POST) && !empty($_POST) && $_POST != ''){
    $_SESSION['postdata'] = $_POST['data'];
}else{
    $_SESSION['postdata'] = $_SESSION['postdata'];
}

I can confirm at the time of calling the service the right values are being sent (I inspected through the console).

Do you think a slight delay in tab opening would help?

developer10
  • 1,450
  • 2
  • 15
  • 31

1 Answers1

0

A "long enough" delay in tab opening will help but it's not a good way to do it. You should use the $window.open as a callback for sendData and open the tab only when the service completed it's job.

Adi Fatol
  • 956
  • 15
  • 24
  • I implemented as advised. Now it does not open in a new tab but rather in a big pop-up window. However, my post data is still not present in that new window and it should be because that is my main problem here. – developer10 Feb 02 '15 at 08:11
  • Does your original code work with long enough delay (like 5 ~ 10 seconds)? – Adi Fatol Feb 02 '15 at 08:28
  • Update: Some improvement has been made with deferring the service. In the window that pops up, I receive the correct post data. I tired going to my old way (opening in a new tab) and it falls back to the values saved in session. That means my way, unlike your way, is not receiving postdata because in my php file I have that `if` condition somewhat changed and it now surely evaluates correctly based on whether there are `$post`data or not. Now if I'm just able to get rid of this big pop-up window and have it opened in a new tab... – developer10 Feb 02 '15 at 08:50
  • Opening a new window vs a new tab depends on browser's type/version/settings. See here a more detailed discussion about this topic http://stackoverflow.com/questions/15818892/chrome-javascript-window-open-in-new-tab – Adi Fatol Feb 02 '15 at 09:39