php variable within javascript (Sweet Alert) line

Asked

Viewed 1,584 times

0

I’m creating a message using the resource Sweet Alert, and where I need to present the person’s name inside a Alert coming from a variable PHP.So, I wonder how I can do it properly.

Note: . 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 informed the PHP code

text: 'Gostaria de cadastrar os documentos do usuário **<?php echo $usuario ['nome']?>**?', 

1 answer

1


Have you "opened" with the <?php at first, 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>";
  • I used your example 2 concatenating and it worked perfectly. I thank you for your great help Taynan

Browser other questions tagged

You are not signed in. Login or sign up in order to post.