UPDATE PHP with JavaScript

4

I'm trying to perform UPDATE, but it gives the following error:

  

Notice: Undefined index: id in C: \ wamp \ www \ SitePHP \ base \ updateFuncionario.php on line 4. Follow my code

function

function atualizar(id){
    if(confirm("Deseja atualizar o registro?")){
        location.href = 'atualizarFuncionario.php?id=' + id;
    }
}

log.php

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="css/estilo.css">
        <script src="JS/func.js"></script>
        <script src="JS/jquery-3.2.1.min.js"></script>
    </head>
    <body>

        <p class="cab">ALTERAÇÃO DE FUNCIONÁRIOS</p>
        <form action="base/atualizarFuncionario.php" method="POST">

          <?php

             require_once './base/conexao.php';  

               $funcionario = filter_input(INPUT_GET,"id");
               $nome_alter= filter_input(INPUT_GET,"nome");
               $cpf_alter= filter_input(INPUT_GET,"cpf");
               $tel_alter= filter_input(INPUT_GET,"tel");
          ?>



  <center> <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter;?>" required="" placeholder="Nome" ><br><br>
        <input type="text" name="cpf" id="inp" size="40" id="cpf" required="" value="<?php echo $cpf_alter;?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>
        <input type="text" name="tel" id="inp" size="40" value="<?php echo $tel_alter;?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)"  placeholder="Telefone(xx)9xxxx-xxxx"><br><br>
        <a href="#" onclick="atualizar(<?php $funcionario = $_GET['id'];?>)"><button type="submit">ATUALIZAR</button></a></center>

        </form>
    <center> <a href="base/listarFuncionario.php"><input type="submit" value="Listar"</a> </center>


    </body>
</html>

updateFunctional.php     

include_once './conexao.php';
$id = $_GET['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$telefone = $_POST['tel'];
$atualizar = "UPDATE funcionario SET nome='$nome', tel = '$telefone', cpf= '$cpf'  WHERE id= '$id'";

$atualizar = $PDO->query($atualizar);


if($atualizar){
    echo "<script>alert('Funcionario atualizado com sucesso!');  </script>";
} else {
    echo "<script>alert('Erro ao atualizar'); </script>";
     //location.href = '../formCadastro.php';
}
    
asked by anonymous 23.11.2017 / 20:21

2 answers

1

It does not make sense to JavaScript there, you do not need to use it, just put a hidden field in your form as the id that will update and send as "POST" same to PHP, it would look like this:

log.php:

   <!DOCTYPE html>

    <html>
            <head>
                <meta charset="UTF-8">
                <link rel="stylesheet" type="text/css" href="css/estilo.css">
    <script src="JS/func.js"></script>
    <script src="JS/jquery-3.2.1.min.js"></script>
    </head>
        <body>
            <P class="cab">
               ALTERAÇÃO DE FUNCIONÁRIOS
            </P>
            <form action="base/atualizarFuncionario.php" method="POST">

                    <?php
        require_once './base/conexao.php';            
        $funcionario = filter_input(INPUT_GET,"id");
        $nome_alter= filter_input(INPUT_GET,"nome");
        $cpf_alter= filter_input(INPUT_GET,"cpf");
        $tel_alter= filter_input(INPUT_GET,"tel");
        ?>



  <center>
    <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter;?>" required="" placeholder="Nome"><br><br>
    <input type="text" name="cpf" size="40" id="cpf" required="" value="<?php echo $cpf_alter;?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>
    <input type="text" name="tel" size="40" value="<?php echo $tel_alter;?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)" placeholder="Telefone(xx)9xxxx-xxxx"><br><br>
    <input type="hidden" name="id" size="40" value="<?php echo $funcionario?>" id="id" >
    <button type="submit">ATUALIZAR</button>
</center>



            </form>
        <center> <a href="base/listarFuncionario.php"><input type="submit" value="Listar"</a> </center>


        </body>
    </html>

and php:

include_once './conexao.php';
$id = $_POST['id'];
$nome = $_POST['nome'];
$cpf = $_POST['cpf'];
$telefone = $_POST['tel'];
$atualizar = "UPDATE funcionario SET nome='$nome', tel = '$telefone', cpf= '$cpf'  WHERE id= '$id'";

$atualizar = $PDO->query($atualizar);


if($atualizar){
    echo "<script>alert('Funcionario atualizado com sucesso!');  </script>";
} else {
    echo "<script>alert('Erro ao atualizar'); </script>";
     //location.href = '../formCadastro.php';
}
    
23.11.2017 / 20:46
2

I changed your form and its POST and what I'm sending is a functional code, that is, it was tested before posting here. Notice that I removed the href from its type='submit' because if it is already being indicated in <form action='...' it is already recognized the file to which it has to submit the data

<form action="atualizarFuncionario.php" method="POST">

    <?php

    require_once './base/conexao.php'; 

        $funcionario = filter_input(INPUT_GET,"id");
        $nome_alter= filter_input(INPUT_GET,"nome");
        $cpf_alter= filter_input(INPUT_GET,"cpf");
        $tel_alter= filter_input(INPUT_GET,"tel");

    ?>

    <label>ID: </label>
    <input type="hidden" name="id" value="<?php echo $nome_alter; ?>" required>

    <label>Nome: </label>
    <input type="text" id="inp" name="nome" size="40" value="<?php echo $nome_alter; ?>" required="" placeholder="Nome"><br><br>

    <label>CPF: </label>
    <input type="text" name="cpf" id="inp" size="40" id="cpf" required="" value="<?php echo $cpf_alter; ?>" maxlength="14" onkeyup="mascCPF(this.value)" placeholder="CPF xxx.xxx.xxx-xx"><br><br>

    <label>Telefone: </label>
    <input type="text" name="tel" id="inp" size="40" value="<?php echo $tel_alter; ?>" id="tel" required="" maxlength="14" onkeyup="mascTel(this.value)"  placeholder="Telefone(xx)9xxxx-xxxx"><br><br>

    <center><input type='submit' name='enviar' value='ATUALIZAR'></center>

</form>

And the code to change was this way, I created a bank and here worked perfectly, where it will be checked if there is ID of the employee to do UPDATE , otherwise, if it does not exist it will INSERT

include_once './conexao.php';

    if(isset($_POST['enviar'])){

        $id = $_POST['id'];
        $nome = $_POST['nome'];
        $cpf = $_POST['cpf'];
        $tel = $_POST['tel'];

        $sql = "SELECT * FROM funcionario WHERE id = '$id' "; 
        $resulta = $conn->query($sql);
        $row = $resulta->fetch_assoc();

        if ($resulta->num_rows > 0) {
            $result = "UPDATE funcionario SET nome = '$nome', cpf = '$cpf', tel = '$tel' WHERE id = '$id' ";
        } else {
            $result = "INSERT INTO funcionario (nome, telefone, endereco) VALUES ('$nome, '$cpf', '$tel')";
        }

        $resultado = mysqli_query($conn, $result);
        echo $result;
     }
    
23.11.2017 / 20:53