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

1

Friends,

CONTEXTUALIZING: I have a php file with an HTML form, which I will call "file1.php" where I pass an id variable via post to it 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" for the user to accept or not the operation that will be performed.

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

PROBLEM: To perform the operation on "file2.php" I need a variable that was typed in the "file1.php" form.

How do I pass the variable in question from "file1.php" to "file2.php" through JS, being able to perform the operation on the database?

Follow the codes below. In file1.php, the operation to be performed happens on lines 34 to 50. And in file 2.php, I can not perform any operations.

CODES:

File1.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>

File2.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');
?>
    
asked by anonymous 15.06.2016 / 01:28

1 answer

0

switch

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

by

window.location.assign("deleteuser.php?id=<?=$id?>");
    
15.06.2016 / 02:51