Passing a body value from a modal to a PHP function

Asked

Viewed 29 times

-2

Good night,

I have the following problem, I have a modal within a system, what I need to do and I’m not getting is to pass the values I read in javascript to a PHP function for in case I "delete" what I need, if someone can please help me, i am new in development and am struggling to keep learning, thank you very much, follow the codes I am using:

This is my job:

<?php 

    function paraProd($id,$maquina){

            $id = $_POST ['id'];
            $maquina = $_POST ['maquina'];

            //  Declara os valores de sessão globais a uma variável!
            $local = $_SESSION['hostname'];
            $user = $_SESSION['idUsuario']; 

            //  Chama função de conexão com banco de dados!

            $conn = DBConnect();

            //  Faz as alterações de UPDATE nas tabelas do banco de dados!

            $sql = "DELETE FROM supervisorio.producao_inj WHERE maquina = '$maquina'";

            if (mysqli_query($conn, $sql)) {
                  echo "Record deleted successfully";
                } else {
                  echo "Error deleting record: " . mysqli_error($conn);
                }



    }

Here is the modal from where I want to send the data to the PHP function:

function modalAceita(id,maquina,lote,produto){

            $.ajax({
            url:"functions.php",
            type:'POST',
            data: {id:id,maquina:maquina,lote:lote,produto:produto},
            beforeSend:function(){
                $('#modal4').find('.modal-body').html('Carregando...');
                $('#modal4').modal('show');
            },
            success:function() {
                $('#modal').modal('hide');
                $('#modal4').find('.modal-title').html("<strong>INJETORA "+id+"</strong>");
                $('#modal4').find('.modal-body').html('<div class="alert alert-danger" role="alert">Tem certeza de parar a Produção?</div><div class="alert alert-warning" role="alert">Maquina: <label>'+maquina+'</label><br/>Produto: <label>'+produto+'</label><br/>Lote: <label>'+lote+'</label><br/></div><div class="row justify-content-md-center"><button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>&nbsp;&nbsp;<button type="button" class="btn btn-primary" onclick="<?php paraProd('+id+','+maquina+');?>" data-dismiss="modal">Salvar mudanças</button></div>');
                $('#modal4').modal('show');
            }
        });

}

I’m sure I’m doing it wrong, but I haven’t found a solution anywhere, I’m sorry if I posted in the wrong way also my question, but I’m new on this journey, thank you from now on to anyone who can help me!

1 answer

0

The only problem I see is that your function paraProd() is not being called, if it is to remove a record from a given table, instead of doing so within functions.php you could create a unique file to delete the record from this table.

ex:

creates a file called delete_table.php (ai calls this file in ajax)

at the file start do an isset validation

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

// here you make the routine of deleting the record or else call its function to()

}

Browser other questions tagged

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