Pass id received from the database by URL with javascript

0

I started to learn javascript, I have a short time and I am quite a lay person.

Then ...

I'm trying to replace alert windows with custom manners. Then when I click the edit button for example, it opens my confirmation window and if I click OK it will redirect me to the editing page.

I would like the redirect of the page to be sent the id of the user caught in the database, passing it through the url with window.location = ''; or another redirector. I do not know if you can do that.

If anyone can help, I'll be grateful.

                               Confirm     

<body>

    <?php foreach($usuario->findAll() as $key => $value): ?>
        <?php 
            $id = $value->id;
            $nome = $value->nome;
            $nascimento = $value->nascimento;
            $sexo = $value->sexo;
            $uf = $value->uf;
        ?>

        <b>Nome:</b> <?php echo $nome; ?> <br>
        <b>Nascimento:</b> <?php echo $nascimento; ?><br>
        <b>Sexo:</b> <?php echo $sexo; ?><br>
        <b>UF:</b> <?php echo $uf; ?><br>

        <?php echo "<button onclick='doAlert();'>Editar</button>"; ?><br><br>

    <?php endforeach; ?>

    <script type="text/javascript">     
        function doAlert(){
            var msg = new DOMAlert({
                title: 'Confirme',
                text: 'Mensagem de alerta - testes.',
                skin: 'default',
                width: 300,             
                ok: {value: true, text: 'Ok', onclick: open},
                cancel: {value: false, text: 'Cancelar', onclick: close }
            });
            msg.show();
        };

        function close(sender, value){
            sender.close();             
        }
        function open(sender, value){
            /*PEGAR ID DO USUARIO E PASSAR VIA URL */
            window.location='';             
        }
    </script>

</body>

    
asked by anonymous 18.04.2017 / 17:04

1 answer

0

Try using Sweet Alert 2

    <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/sweetalert2/6.6.0/sweetalert2.css">
<body>

    <?php foreach($usuario->findAll() as $key => $value): ?>
        <?php 
            $id = $value->id;
            $nome = $value->nome;
            $nascimento = $value->nascimento;
            $sexo = $value->sexo;
            $uf = $value->uf;
        ?>

        <b>Nome:</b> <?php echo $nome; ?> <br>
        <b>Nascimento:</b> <?php echo $nascimento; ?><br>
        <b>Sexo:</b> <?php echo $sexo; ?><br>
        <b>UF:</b> <?php echo $uf; ?><br>

        <?php echo "<button onclick='doAlert(".$id.");'>Editar</button>"; ?><br><br>

    <?php endforeach; ?>

    <script src="https://code.jquery.com/jquery.js"></script><scriptsrc="https://cdn.jsdelivr.net/sweetalert2/6.6.0/sweetalert2.js"></script>

    <script type="text/javascript">     
        function doAlert(id){
            swal({
              title: 'Tem certeza ?',
              text: "Tem certeza que deseja editar esse usuário ?",
              type: 'warning',
              showCancelButton: true,
              confirmButtonColor: '#3085d6',
              cancelButtonColor: '#d33',
              confirmButtonText: 'Sim, editar!'
            }).then(function () {
              window.location.href='pagina_de_edicao.php?id='+id
            })
        };
    </script>

</body>
    
18.04.2017 / 17:23