Problem with post/get method to update table

Asked

Viewed 103 times

2

I’m trying to update my table using javascript so I don’t have to leave the screen every time I turn off a field, my table: inserir a descrição da imagem aqui

The button I used in PHP but went to another page:

<td class="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 for 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 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);

I click the button and nothing happens :T

  • Every button has the same id='botao-olho'? If yes, it’s wrong, it should be class='botao-olho'.

  • That one <?php echo $id ?> seems wrong to me too. The id on the date of Ajax should be dynamic according to the button clicked.

  • I switched to class and did not change anything...the id comes from mysql that I use to popular table

  • What is this function: onclick='myFunction()'?

  • I do not understand javascript, as I saw the on click on the script, I put inside the button, no need?

  • No need. Where the HTML id you want to send?

  • I edited the question with the button that works with php

Show 3 more comments

1 answer

1


You can’t use the same id on all buttons. A id should be unique on the page. So change the id for class and remove the onclick which has no function:

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

And add a data-id to get the id you want to send by clicking button, concatenating the variable $id of PHP:

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

Now Ajax you will use only:

$(".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 PHP requested by Ajax you put the ID in lower case in GET:

$id = $_GET['id'];

In the Ajax options you have reversed the values of type and dataType, but you don’t even need these options since by default Ajax sends via GET and you don’t expect any feedback, just send the id. You don’t need the .done, just one success to remove the line where the button is.

  • 1

    It worked first! Show!

  • i tried to update a table, to change the quantity, to send the id ok, how do I send the amount in the javascript date? Thanks a lot Sam

  • this, would be in another table to change the quantity, but the process is the same

  • Would look like this: data: {id: $this.data("id"), quantidade: $(this).closest("tr").find("td:eq(2)").text().trim() },

  • It’s another table Sam, much simpler, and has a change button on it instead of inactivating, as I put a date-Qtd = $quantity , all right?

  • can be tb, it gets easier to catch with $this.data("qtd")

  • Dude.. doesn’t change, opens a chat.. is 3 lines and I think it solves :(

  • blz... let me see if I can shake the chat

Show 4 more comments

Browser other questions tagged

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