PHP - JAVASCRIPT - Take a form obtained variable to another PHP page

Asked

Viewed 79 times

1

Friends,

CONTEXTUALIZING: I have a php file with an HTML form, which I will call "arquivo1.php" where I pass an id variable via post to the same one where I handle the data.

In this file1, I perform some operations that require user acceptance. I created a script in JS that launches on the screen a "confirm" so that the user accepts or not the operation that will be performed.

If the user accepts the "confirm" of this "arquivo1.php", I redirect via JS (window.location.href) to another php file, called "arquivo2.php", where I do operations in a database.

PROBLEM: To perform the operation in "arquivo2.php" I need a variable that was typed in the form of "arquivo1.php".

As I pass the variable in question from "arquivo1.php" to "arquivo2.php" through JS, I can perform the operation in the database?

Follow the codes below. In the file 1.php, the operation to be performed happens in lines 34 to 50. And in archive2.php, I can’t perform any operation.

CODES:

Arquivo1.php

    <?php
include_once "sessao.php";
require 'db/DB.class.php';
//inicia a conexão
$db = new DB( 'vicktorjunior',                    //usuario
    '123',                           //senha
    'db_victorjr',                   //banco
    'webacademico.canoas.ifrs.edu.br'//servidor
);

extract($_REQUEST);//transformando os dados enviados em variaveis


if(isset($_REQUEST['acao'] )) {
    $acao = $_REQUEST['acao'];
} else {
    $acao='';
}


//$id = ''; //inicia as variaveis de controle vazia.
//$acao = 'insercao';
if(isset($usuario)){
    if($acao != 'atualizarFim') {
        $dados[0] = $senha;
        $dados[1] = $usuario;
        $dados[2] = $email;

        $db->execute("INSERT INTO usuario (senha, usuario, email) VALUES (?,?,?)", $dados);
    }
}

//deletar
if(isset($id) && $acao == 'deletar'){
    $dados2[0] = $id;

    //$db->execute("DELETE FROM personagens WHERE id_usuario=?",$dados2);
    $op = $db->execute("DELETE FROM usuario WHERE id_usuario=?",$dados2);


    if (!$op) { ?>
        <script> if(confirm('Usuário tem personagens cadastrados! Deseja remover os personagens cadastrados?')){
                window.location.href='deleteuser.php?id=<?php $id?>';
            } else {

            }

        </script>
<?php
    }

}
$dadosTemp['usuario'] = '';
$dadosTemp['email'] = '';
$dadosTemp['senha'] = '';

if(isset($acao) && $acao == 'atualizarFim'){
    $dados[0] = $senha;
    $dados[1] = $usuario;
    $dados[2] = $email;
    $dados[3] = $id;

    $db->execute("UPDATE usuario SET senha=?, usuario=?, email=? WHERE id_usuario=?", $dados);

    $acao = '';
}

if(isset($acao) && $acao == 'atualizar'){
    $consulta = $db->query("SELECT * FROM usuario
                        WHERE id_usuario = $id");

    foreach ($consulta as $linha) {
        $dadosTemp = $linha;
    }
    $acao = 'atualizarFim';
}

?>

<!DOCTYPE html>

<html>
<head>
    <meta charset="UTF-8">
    <link href="css/bootstrap.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <title>RPG-BD</title>
</head>
<body>

<div class="container-fluid">
    <header class="row">
        <div class="col-md-12">
            <h1>Medieval RPG - Lista de Usuários</h1>
        </div>
    </header>
    <div role="main">
        <div>
            <div>
                <? if(isset($id) && isset($acao)) { ?>
                <form role="form" action="crudusuario.php?acao=<?= $acao?>&id=<?=$id?>" method="post">
                    <? } else { ?>
                    <form role="form" action="empresa.php" method="post">
                        <? } ?>
                        <ul class="list-group">
                            <li class="list-group-item">
                                <input type="text" name="usuario" id="usuario" value="<?=$dadosTemp['usuario']?>" required placeholder="usuário">
                                <input type="email" name="email" id="email" value="<?=$dadosTemp['email']?>" required placeholder="email">
                                <input type="password" name="senha" id="senha" value="<?=$dadosTemp['senha']?>" required placeholder="senha">
                            </li>
                            <li class="list-group-item"><button type="submit" class="btn-default">Inserir - Atualizar</button></li>
                            <li class="list-group-item"> <button class='btn-default' onclick="location.href='prod.php'">Voltar</button>
                                <button class="btn-default" onclick="location.href='deslogar.php'">LOGOUT</button></li>
                        </ul>
            </div>
                </form>


        </div>

            <div class="panel-body">
                <table class="table table-responsive table-hover">
                    <thead>
                    <tr>
                        <th>Botões</th>
                        <th>Usuário</th>
                        <th>E-mail</th>
                    </tr>
                    </thead>
                    <tbody>
                    <ul class="list-group">
                        <?php
                            $consulta = $db->query("SELECT * FROM usuario ORDER BY id_usuario DESC");
                            foreach ($consulta as $linha) {
                            ?><tr>
                            <td>
                                <a class="glyphicon glyphicon-trash" href="?id=<?= $linha['id_usuario']?>&acao=deletar">Deletar</a>
                                <a class="glyphicon glyphicon-refresh" href="?id=<?= $linha['id_usuario']?>&acao=atualizar">Atualizar</a>
                            </td>
                                    <td align = "center"><?= $linha['usuario']?></td>
                                    <td align = "center"><?= $linha['email']?></td>
                                </li>
                                </tr>
                            <?php
                            }
                            ?>
                    </ul>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

<footer class="row">
    <h6>RN Dev &copy; Todos os direitos reservados</h6>
</footer>
</body>
</html>

Arquivo2.php:

<?php
include_once "sessao.php";
require 'db/DB.class.php';
//inicia a conexão
$db = new DB( 'vicktorjunior',                    //usuario
    '123',                           //senha
    'db_victorjr',                   //banco
    'webacademico.canoas.ifrs.edu.br'//servidor
);
extract($_REQUEST);
$dados2[0] = $id;
$db->execute("DELETE FROM personagens WHERE id_usuario=?",$dados2);
$op = $db->execute("DELETE FROM usuario WHERE id_usuario=?",$dados2);

header('location:crudusuario.php');
?>

1 answer

0

change

window.location.href='deleteuser.php?id=';

for

window.location.assign("deleteuser.php?id=<?=$id?>");
  • 2

    Hi Eric, welcome! You can explain the answer to get more useful and clear the differences you suggest?

Browser other questions tagged

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