Send button id by ajax to update database

Asked

Viewed 54 times

0

Good night, you guys,

I’m a beginner in the area, and I ask you to help me, I have a table with php database results, in one of the fields has a button that stores the id (primary key) and clicking that button calls a modal window, and I wanted to add that by clicking this id button, it updated a status from a field of 0 to 1 in the database. I’m trying to update with ajax, but it’s not working out, who can help me thank you mto.

My code is like this:

 //Tabela
          <table class="table">      

            <thead>
              <tr>  
              <td><center>Visualizar</center></td>

              <td><center>De</center></td>

              <td><center>Assunto</center></td>

              <td><center>Data</center></td>

              </tr>
            </thead>
            <tbody>
               <?php
                include "config.php";
                $stmt = $db->prepare("select id,msg_assunto,msg_text,lida_para from mensagens");
                $stmt->execute();
                while($row = $stmt->fetch()){
                  if($row > 0){ 
                ?>
                <tr>   
                  <td><center><button type="button" id="btn-destaque" class="btn btn-xs" data-toggle="modal" data-target="#modalLRForm<?php echo $row['id']; ?>"><img src="img\pesquisa.png"></button></center></td>              
                  <td><?php echo $row['msg_de']?></td>
                  <td><?php echo $row['msg_assunto']?></td>
                </tr>
                </tbody>
               </table>

//ajax
                 <script>
 $("#btn-destaque").click(function () { //Quando o botão destaque for pressionado
var idAnun = $row['id']; //Aqui id do botão
$.ajax({
    method: "post", //Escolhe o método de envio
    url: "/fetch.php", //O endereço do script php que fará o update
    data: {id: idAnun} //Envio da ID do anuncio e a ação (ativar)

}).done(function (answer) {
    alert("sucesso");

}).fail(function (jqXHR, textStatus) {
    alert("falhou");
});
});
 </script>

My fetch.php:

<?php

 include("config.php");

 if (isset($_POST['id']) && $_POST['id'] != 0) { //$_POST['id'] != 0 (em caso de tabela auto incremento o id será do tipo inteiro e nunca será iniciada em 0) isso em MySQL não sei outros Bancos de dados
$db = connectdb(); //função do "script_BD.php"

$update = $db->prepare("UPDATE mensagens SET lida='1' WHERE id=:id");
$update->bindValue(":id",$_POST['id'],PDO::PARAM_INT);
$update->execute(); // executa update;
}

1 answer

1


You are making AJAX requests using protocol HTTP with Javascript language in frontend:

$.ajax({
    method: "post", //Escolhe o método de envio
    url: "/fetch.php", //O endereço do script php que fará o update
    data: {id: idAnun} //Envio da ID do anuncio e a ação (ativar)

}).done(function (answer) {
    alert("sucesso");

}).fail(function (jqXHR, textStatus) {
    alert("falhou");
});

It would be interesting to test the sending of these requests and what are the answers obtained in sending these requests to the server. For this, you can use some HTTP request testing tools, a well-known is the Postman.

Where is the 'mistake'? Try debugging using Devtools from the browser you are using, usually the F12 key enables this tool. Go to the 'Network' tab, there you can view the requests that the page is making.

Chrome DevTools

Before we solve we need to understand what is happening.

  • 1

    Thank you I managed to identify the error and I tidied up, but thank you!

Browser other questions tagged

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