Button needs to simultaneously call an AngularJS method and open a new tab carrying $POST data

260 Views Asked by At

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?

1

There are 1 best solutions below

4
Adi Fatol On

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.