Passing variable to modal

Asked

Viewed 1,517 times

2

I have a modal that opens at the end of an action (Example: Disable User). I am building the modal and wanted it to receive the text present in an ajax variable. Check code:

 $.ajax({
        url: "desativarUsuario.php",
        type: 'post',
        data: {
            codigosDesativar: paraDesativar.map(function () {
                return this.cod_user
            }).get()
        },
        success: function (resposta) {
        resposta = JSON.parse(resposta);
         console.log(resposta, typeof resposta, typeof resposta[0], resposta[1]);
        if (resposta[0]) {
          var tabela = $('#tableUsuario tr');

          $.each(tabela, function(index, tr) {
            var checkbox = $(tr).find("input:checkbox");
            if ($(checkbox).is(':checked')) {
              $(tr).find(".status").text("Inativo");
              $(tr).find(".status").css("color", "#BD362F");
              $(tr).find(".inativo").addClass('hide');
              $(tr).find(".ativo").removeClass('hide');
            } 
          });
        }
        alert(resposta[1]);
    }
    });

The variable resposta returns: Array [ true, "Usuário foi desativado com Sucesso!" ]

How to move inside the modal below this message "User has been Successfully Disabled!":

<div id="myModal" class="modal fade">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">Confirmation</h4>
            </div>
            <div class="modal-body">
                <p>AQUI QUERIA A MENSAGEM@!!!!!!</p>
                <p class="text-warning"><small>If you don't save, your changes will be lost.</small></p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary">Save changes</button>
            </div>
        </div>
    </div>
</div>

1 answer

2


You can add this to your Success

$(".modal-body p").html("Usuário foi desativado com Sucesso!");

using your array

 $(".modal-body p").html(resposta[1]);
  • Just one other related question: if($ativar==2){<script>$(".modal-body p").html("Selecione pelo menos um usuário que deseja desativar!");&#xA; $(document).ready(function(){&#xA; $("#modalMsgAviso").modal('show');&#xA; });</script>} reports missing error '<'. What may be?

  • Yes, you can say I’m here?

  • Why did you use that <script> tag inside if ? Your code should already be inside one, you don’t need to put another.

  • I edited the @vcrzy

  • Oops. It worked.?

  • My code is not inside a script tag. It’s inside a PHP. I should take it anyway?

  • Here’s what you do. Take the $(".modal-body p"). html("Select at least one user you want to deactivate!"); and put inside the ready Ocument as well. Then you echo everything. For example: if($activate==2){ echo '<script>your code here</script>'; If it doesn’t work out, I suggest opening another question or putting it online, so I can see it and help you solve it. I’m going to lunch and then we’re there, thanks

  • It worked, man. Thank you :D

  • hahahaha for nothing. open

Show 4 more comments

Browser other questions tagged

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