How to pass value within the modal with PHP

Asked

Viewed 3,670 times

0

I am trying to create a delete button for a table. Deletion will be ugly in two steps: 1. click on the bin 2. confirms

I believe I’m getting confused when passing the value of $id to modal.

<html lang="en">
<head>
    <title>Estoque</title>
    <!-- Bootstrap core CSS -->
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <!-- Bootstrap theme -->
    <link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
    <script src="bootstrap/js/bootstrap.min.js" </script>
    <script src="jquery-2.1.4.min.js"</script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>
    <div class="container">  
        <table class="table">
            <tr><b>
                <td>Código Produto</td>
                <td>Descrição</td>
                <td>Preço</td>
                <td>Quantidade Estoque</td>
            </tr></b>

    <?php
        REQUIRE_ONCE "conexao.php";
        $sql = "SELECT id, cod_produto, dsc_produto, preco_produto, qtd_estoque, qtd_limitador FROM estoque";
        $result = mysqli_query($conexao, $sql);
        while ($row = mysqli_fetch_assoc($result)){ 
    ?>

            <tr bgcolor="<?php echo $bg ?>">
                <td><?php echo $row[cod_produto] ?></td>
                <td><?php echo $row[dsc_produto] ?></td>
                <td><?php echo $row[preco_produto] ?></td>
                <td><?php echo $row[qtd_estoque] ?></td>
                <td><?php echo $row[qtd_limitador] ?></td>
                <td>
                    <form method="get" action="edit.php">
                        <button type="hidden" name="id" class="btn btn-default" value="<?php echo $row[id]?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </button>
                    </form>
                    <form method="POST" action="<?php $_SERVER['PHP_SELF']; ?>">
                        <button type="button" name="botaoDelete" class="btn btn-default" data-toggle="modal" value="<?php echo $row[id] ?>" data-target="#myModal"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
                    </form>
                </td>
            </tr>

        <!-- Modal -->
        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog">
        <div class="modal-content">
        <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title">Excluir produto</h4>
      </div>
      <div class="modal-body">
        <p>Você tem certeza que deseja excluir?</p>

      </div>
      <div class="modal-footer">
        <form method="POST">
            <button type="button" name="botaoConfirma" value="<?php echo $dlt ?>" class="btn btn-danger">Excluir</button>
            <button type="button" class="btn btnn-default" data-dismiss="modal">Fechar</button>
        </form>

    <?php      
        if (isset($_POST["botaoDelete"])) {
            $dlt = $_POST["botaoDelete"];
            if (isset($_POST["botaoConfirma"])) {
                $cnf = $_POST["botaoConfirma"];
                $deleteSql = mysqli_query($conexao, "DELETE * FROM estoque WHERE id='".$cnf."'");
            }
        }
    }   
    ?> 

    </div>
    </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->
    </tbody>
    </div>
    </body>

Is there any way to pass button value without using form? Also, how can I call this value (if the button is pressed) without using if (isset)? Worth it?

  • What’s the problem that’s happening?

  • It is not deleting the desired line, IE, I am not able to pass the ID until the "confirm" button. Does it pay to use java in this part even focusing on PHP development? I’m still a beginner and it confuses me.

  • There are several ways to solve this, the simplest way to do this would be to use the confirm() from javascript, separate the deletion of the record from file to part (maybe in a later function ;]) if the response from confirm() for true vc redirect to this new file (delete.php) if not only one return false in its javascript function.

  • But if I do this function outside of the file, will it need to leave the page or can I do everything in the modal? As I said before, it wouldn’t be a good practice to try this with php since the code is basically all in php?

  • It will leave the page if the deletion is confirmed, start this way, then you can do it via ajax(ai yes does not leave the screen nor the refresh on the screen).

2 answers

3

Oh, from what I understand, this is what you do. Next:

1st: In this step you call the modal and, in the method onclick, you pass the id you want. You will put on the delete link button:

<a href="#deletar-dado" role="button" data-toggle="modal" onclick="deletaDado(<?php echo $id ?>)" class="deletar">Excluir</a>

2º: Creates your modal as follows:

<div id="deletar-dado" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
            <h4 class="modal-title">Confirmação</h4>
        </div>
        <div class="modal-body">
            <p>Deseja realmente excluir este registro?</p>
        </div>
        <div class="modal-footer">
            <button type="button" data-dismiss="modal" class="btn default">Cancelar</button>
            <a id="confirmaDelecao" class="btn red">Apagar</a>
        </div>
    </div>
</div>

3rd: create a function that js that will receive the data id to delete:

<script type="text/javascript">
function deletaDado (idDado){
    //seta o caminho para quando clicar em "Apagar".
    var href = $('#confirmaDelecao')[0].baseURI + '/deletar/' + idDado;
    //adiciona atributo de delecao ao link
    $('#confirmaDelecao').prop("href", href);
}
</script>

If you need anything, call us there. D Thanks!

  • I don’t understand very well javascript, but I don’t have to call this function? I tested here what you said the confirmation button does nothing.

  • you call her inside the onclick, there on the first step

  • I’m having.php/delete/Undefined test in the URL as a return.

  • You are putting the id inside the onclick?

0

Following Matheus' lead, I obtained:

<html lang="en">
<head>




   <title>Estoque</title>

<!-- Bootstrap core CSS -->
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
<!-- Bootstrap theme -->
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet">
<script src="bootstrap/js/bootstrap.min.js" </script>
<script src="jquery-2.1.4.min.js"</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>
<body>


<div class="container">  
        <table class="table">
            <tr><b>
                <td>Código Produto</td>
                <td>Descrição</td>
                <td>Preço</td>
                <td>Quantidade Estoque</td>
            </tr></b>


<?php
REQUIRE_ONCE "conexao.php";
$sql = "SELECT id, cod_produto, dsc_produto, preco_produto, qtd_estoque, qtd_limitador FROM estoque";
$result = mysqli_query($conexao, $sql);
    while ($row = mysqli_fetch_assoc($result)) 
        { 

?>


            <tr bgcolor="<?php echo $bg ?>">
                <td><?php echo $row[cod_produto] ?></td>
                <td><?php echo $row[dsc_produto] ?></td>
                <td><?php echo $row[preco_produto] ?></td>
                <td><?php echo $row[qtd_estoque] ?></td>
                <td><?php echo $row[qtd_limitador] ?></td>



                <td>
                    <form method="get" action="edit.php">
                        <button type="hidden" name="id" class="btn btn-default" value="<?php echo $row[id]?>"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span> </button>
                    </form>
                    <a href="#deletar-dado" role="button" data-toggle="modal" onclick="deletaDado(<?php echo $row[id]?>)" class="deletar">Excluir</a>
                </td>
                </tr>



    <!-- Modal -->
    <div id="deletar-dado" class="modal fade" tabindex="-1" data-backdrop="static" data-keyboard="false">
<div class="modal-dialog">
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
        <h4 class="modal-title">Confirmação</h4>
    </div>
    <div class="modal-body">
        <p>Deseja realmente excluir este registro?</p>
    </div>
    <div class="modal-footer">
        <button type="button" data-dismiss="modal" class="btn default">Cancelar</button>
        <a id="confirmaDelecao" class="btn red">Apagar</a>
    </div>
</div>
</div>


</tbody>
</div>
<?php      

}   
?> 
<script type="text/javascript">
function deletaDado (idDado){
//seta o caminho para quando clicar em "Apagar".
var href = $('#confirmaDelecao')[0].baseURI + '/deletar/' + idDado;
//adiciona atributo de delecao ao link
$('#confirmaDelecao').prop("href", href);
}
</script>
</body>

By clicking the delete link on the confirmation screen, nothing happens.

  • Man, you gotta change your href inside the delete function.

  • Sorry, but I still don’t understand the javascript syntax very well. Could you give more details about this? If possible explain what each element is doing in the function, maybe this helps more people too.

  • you managed to solve in this way?

  • I’m actually trying to use Sweetalert instead, but I’m still having some problems.

Browser other questions tagged

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