Pass id received from the database through the javascript URL

Asked

Viewed 846 times

0

I started learning Javascript a little while ago and I’m very lay yet.

So...

I’m looking to replace the Alert windows with custom fashions. Then by clicking the edit button for example, opens my confirmation window and if I click OK it redirects me to the edit page.

I would like the page redirect to send the user id picked up in the database, passing it through the url with window.Location='; or another redirector. I don’t even know if you can do this.

If anyone can help I’d 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>

  • 1

    maybe one of these links will help: https://jsfiddle.net/2pmn1ems/3/, https://answall.com/questions/130803/passar-id-de-um-dado-de-uma-tabela-para-a-modal

  • 1

    Hello, it worked perfectly. Thank you very much!

  • glad it helped. You’re welcome

1 answer

0


Try using the 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>
    <script src="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>

Browser other questions tagged

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