How do I update a table by ID? [closed]

1

I have a table and in it I have a icon with event onClick , the script is doing its job to change the status of true to false , if clicked.

Is my job updating this table every time this happens?

function Change(val){
    $.ajax({
        type: "POST",
        data:{idsocial :val},
        url: "change_01.php",
        success: function(){

       }
    });
}
    
asked by anonymous 22.05.2014 / 05:28

2 answers

1

You have to change your database with a change_01.php update command in your SQL

mysql_query("UPDATE sua_tabela SET campo_atualizado = ".$_POST["estado_campo"]." WHERE idsocial = ".$_POST["idsocial"]);
    
22.05.2014 / 14:32
0

I assumed that what your difficulty is updating the HTML table, not the table in the database.

You can do something like this:

HTML:

<table>
  <tr class='linha' data-id="1">
    <td>Linha 1</td>
    <td class='coluna'>Não</td>
  </tr>
  <tr class='linha' data-id="2">
    <td>Linha 2</td>
    <td class='coluna'>Não</td>
  </tr>
  <tr class='linha' data-id="3">
    <td>Linha 3</td>
    <td class='coluna'>Não</td>
  </tr>
  <tr class='linha' data-id="4">
    <td>Linha 4</td>
    <td class='coluna'>Não</td>
  </tr>
</table>

The data-id attribute contains id for that row in the database.

Javascript / jQuery:

$(".linha").click(function() {
    $(this)
    .children(".coluna")
    .html("Atualizado: " + $(this).data("id")); 
});

The $(this).data("id") function above returns the data-id of that row. You will use this in your AJAX request.

The $(this).children(".coluna") is to get the specific column for that row.

View this example in JSFiddle .

You can change this so everything happens to the column click or an image inside it if you want. (Use $(this).parents(".minha-classe") to get a parent tag if needed).

Read here about Unobtrusive Javascript .

    
22.05.2014 / 15:56