How to get the database id of each TD from the html table

Asked

Viewed 177 times

1

Goodnight

I have a button that holds the bank id of each <td> in a row, I send this id to open a file in another tab, and now I’ve added a second function on this button to get the id in the Subject column that stores the id and update via ajax.

It is working correctly, only the problem is that ajax only updates with the 1 record of row 1 of the table, and with the other rows it simply does not update.
Example: if I click the record number 20 it updates the record with the table id 1. I need to know where my bug is, and why doesn’t it grab the id of every line I click to do the update? I’ve already cracked my head and nothing. Could someone help me?

My code is like this:

<tr>   
<!--  Aqui é a coluna que guarda o id de cada registro   -->          
          <td><input type='hidden' name="subject" id="subject" value="<?php echo $dados['id']?>"><?php echo $dados['id']?></td>
<!--  Aqui o botão que tem duas funções -->
          <td><a href="visualizar.php?protocolo=<?php echo $dados['id'] ?>" type="submit" onclick="inserir_registo()" class="btn btn-primary" target="_blank">Visualizar</a></td>
     </tr> 

<!-- Meu Ajax: -->
 <script>
  function inserir_registo(){

    //dados a enviar, vai buscar os valores dos campos que queremos enviar para a BD
    var dadosajax = {
        'subject' : $("#subject").val()
    };
    pageurl = 'update.php';
    $.ajax({

        //url da pagina
        url: pageurl,
        //parametros a passar
        data: dadosajax,
        //tipo: POST ou GET
        type: 'POST',
        //cache
        cache: false,
        error: function(){
            alert('Erro: Atualizar Registo!!');
        },
        //retorna o resultado da pagina para onde enviamos os dados
        success: function(result)
        { 
            //se foi inserido com sucesso
            if($.trim(result) == '1')
            {
               alert("O seu registo foi atualizado com sucesso!");
               window.location.reload();
            }
            //se foi um erro
            else
            {
                alert("Ocorreu um erro ao atualizar o seu registo!");
            }

        }
    });
}
        </script>

And here’s my update.php page that updates the records:

<?php

 include("conexao.php");
    $id = $_REQUEST['subject'];

    try
    {
        //insere no BD
        $sql = "UPDATE registros SET status=1 WHERE id = '$id'";
        $result = mysqli_query($con, $sql);

        //retorna 1 para no sucesso do ajax saber que foi com inserido sucesso
        echo "1";
    } 
    catch (Exception $ex)
    {
        //retorna 0 para no sucesso do ajax saber que foi um erro
        echo "0";
    }
?>

I’m sorry if I couldn’t explain it properly, I’m a beginner, I’m starting to study these languages now. Who can help me thank you.

  • You know you can’t repeat an id on the same page, right?

  • @Sam what do you mean repeat the same id? , each td of the table has its own id, and the button is taking the id I saved on it by sending it to the page view.php, and I used ajax on that same button to get the id of the table column and update it separately, on another page, update.php

  • It seems to me you’re repeating the id="subject". This cannot. An id must be unique.

  • I put this name and id inside this input in the td column <td><input type='hidden' name="subject" id="subject" value="<?php echo $dados['id']?>"><?php echo $dados['id']?></td> to take the ajax, the value of each line, but not repeated, I just put the same name and id

  • But does each line have this input? If so, it is repeating the id.

  • yes there is, but how will I get the id of each line without the input?

Show 2 more comments

1 answer

1


The problem is that you are repeating the id id="subject" and passing the data from AJAX. So you will always catch the first id="subject" page. An id must be unique, but you don’t need to do this, create a column with hidden input to pick up a value via id.

Just pass the id coming from PHP direct on onclick as a parameter and receive this value in the function.

Change the onclick for:

onclick="inserir_registo('<?php echo $dados['id']?>')"

And opening the function to:

function inserir_registo(id){

And the value of 'subject' for:

'subject': id
  • was this msm, ta working perfectly, mto thank you young man! :)

Browser other questions tagged

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