I am trying to send information by AJAX to a PHP and when the response returns, another PHP opens in another tab that will process the returned information.
I have this code
function printAdmLetter()
{
if($("#adm_nomina").val()==""||$("#case_desc").val()==""||$("#boss_actions").val()==""||$('select[name=cause]').val()==null)
{
swal("¡Olvidaste algo!", "La información no está completa.", "error");
return false;
}
else
{
adm_nomina = document.getElementById("adm_nomina").value;
auth = document.getElementById("auth").value;
cause = $('select[name=cause]').val();
case_desc = document.getElementById("case_desc").value;
boss_actions = document.getElementById("boss_actions").value;
$.ajax(
{
url:"./phpLibraries/admLetter.php",
method:"POST",
data:{adm_nomina:adm_nomina,
auth:auth,
cause:cause,
case_desc:case_desc,
boss_actions:boss_actions},
success:function(data)
{
if(data=='X')
{
swal("¡Error!","No se pudo ingresar la información, intentalo nuevamente.","error");
}
else
{
window.open('phpLibraries/printAdmLetter.php?id='+data);
}
}
});
}
}
The problem is that it doesn't do anything, it doesn't open the tab. I did a test with:
window.location.href='phpLibraries/printAdmLetter.php?id='+data;
and that way it works.
It is a behavior that most browsers adopt. The instruction
window#open
will be blocked if it is not the result of some direct interaction with the user (for example some eventclick
). A code that is executed by an asynchronous event, such as a callback, does not classify as direct interaction with the user, thus stopping the execution ofwindow#open
.It is not very elegant but you could create a link with the empty href and pass the address with jquery:
With Jquery you fill the attribute and then simulate a click
It's not very elegant, as I said, but it could work.