php variable within line javascript (Sweet Alert) PHP

0

I'm creating a message using the Sweet Alert feature, and where I need to display the person name inside an Alert from a PHP variable >. So, I would like to know how I can do it correctly.

Obs :. The connection to the database is working perfectly.     

            echo "<script>    
                    swal({   
        title: 'Usuário Cadastrado.',   
        text: 'Gostaria de cadastrar os documentos do usuário **<?php echo $usuario ['nome']?>**?',   
        type: 'success',   
        cancelButtonText: 'Cancelar',
                    showCancelButton: true,   
        confirmButtonColor: '#00c292',   
        confirmButtonText: 'Sim, Cadastrar',   
        closeOnConfirm: true 
    }, function(){   
        window.location.href = 'novo_usuario'; 
    });

                </script>";
  }
      ?>

Notice that on the line I reported the PHP code

text: 'Gostaria de cadastrar os documentos do usuário **<?php echo $usuario ['nome']?>**?', 
    
asked by anonymous 27.12.2017 / 17:36

1 answer

1

You have already "opened" with <?php at the beginning, you do not need to open again just concatenate with the variable

echo "<script>
      swal({   
        title: 'Usuário Cadastrado.',   
        text: 'Gostaria de cadastrar os documentos do usuário ** {$usuario ['nome']}**?',   
        type: 'success',   
        cancelButtonText: 'Cancelar',
                    showCancelButton: true,   
        confirmButtonColor: '#00c292',   
        confirmButtonText: 'Sim, Cadastrar',   
        closeOnConfirm: true 
    }, function(){   
        window.location.href = 'novo_usuario'; 
    });

                </script>";

Or

echo "<script>
          swal({   
            title: 'Usuário Cadastrado.',   
            text: 'Gostaria de cadastrar os documentos do usuário ** " .$usuario ['nome'] ."**?',   
            type: 'success',   
            cancelButtonText: 'Cancelar',
                        showCancelButton: true,   
            confirmButtonColor: '#00c292',   
            confirmButtonText: 'Sim, Cadastrar',   
            closeOnConfirm: true 
        }, function(){   
            window.location.href = 'novo_usuario'; 
        });

                    </script>";
    
27.12.2017 / 17:57