Problem with post / get method to update table

2

I'm trying to perform an Update on my table through javascript so I do not have to leave the screen every time I inactive a field, my table:

ThebuttonIusedinPHPbutwenttoanotherpage:

<tdclass="center">

    <?php echo"<a class='btn btn-danger' href='update_produto.php?ID=$id'>Inativar</a>";?>

</td>

My button:

<button id='botao-olho' onclick='myFunction()'>Inativar</button>"

Here is the script that goes to the update

<script type="text/javascript">

var btn_olho = $("#botao-olho");

btn_olho.on("click", function(){
    $.ajax({
        url: "update_produto.php",
        type:"html",
        dataType: "GET",
        data: {id: <?php echo $id ?>}
    }).done(function(retorno){

    });

});

here is the update:

$id = $_GET['ID'];
$estado = 'Inativo';
$sql = "UPDATE MASTER_COLETORES SET ESTADO = '$estado' WHERE ID = $id";
$stmt = oci_parse($conexao, $sql);
$result = oci_execute($stmt);

Click the button and nothing happens: T

    
asked by anonymous 19.11.2018 / 12:27

1 answer

1

You can not use the same id on all buttons. A id must be unique on the page. So change the id by class and remove the onclick that has no function whatsoever:

<button class='botao-olho'>Inativar</button>

And add a data-id to get the id you want to send from the clicked button, concatenating the PHP variable $id :

<?php echo "<button data-id='".$id."' class='botao-olho'> Inativar </button>"; ?>

Now Ajax will only use:

$(".botao-olho").on("click", function(){
   var $this = $(this); // salva o elemento numa variável
    $.ajax({
        url: "update_produto.php",
        data: {id: $this.data("id")}, // envia o id
        success: function(){
           $this.closest("tr").remove(); // remove a linha se ocorreu tudo ok
        }
    });
});

In Ajax-requested PHP you put the lowercase ID in GET:

$id = $_GET['id'];

In Ajax options you have inverted the values of type and dataType , but you do not even need these options since by default Ajax sends via GET and you do not expect any return, just send id . You also do not need .done , just a success to remove the line where the button is.

    
19.11.2018 / 13:05