How to update a table by ID?

Asked

Viewed 722 times

1

I have a table and in it I have a icon with the event onClick, the script until then is doing his job which is to change the state of true for false, if this is clicked.

My job is to update this table whenever it occurs?

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

       }
    });
}
  • 2

    Update what exactly? I don’t understand.

  • From what I understand, in the file change_01.php, you will have to make a UPDATE at the bank, using the id and value information sent by ajax...

  • 3

    I think the "table" you refer to is an HTML table... The question is ambiguous, basic information about what you are doing and what or how you intend to do it is missing.

  • Welcome to Stack Overflow! If you can explain better the problem you have in particular and, if possible, show code you have done where this problem is. Your question is too broad, see Help Center How to Ask.

  • @user8828 Edit the question by making it clear what you really want. Mainly inform if the table you refer to is an HTML table or server database table.

2 answers

1

You have to in your file change_01.php make the database change via a command SQL update

mysql_query("UPDATE sua_tabela SET campo_atualizado = ".$_POST["estado_campo"]." WHERE idsocial = ".$_POST["idsocial"]);

0

I assumed what your difficulty is to update 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 attribute data-id contains the id referring to that line in the database.

Javascript/jQuery:

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

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

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

See this example in Jsfiddle.

You can change this to make everything happen by clicking the column or an image inside it if you want. (Use $(this).parents(".minha-classe") to get a parent tag if necessary).

Read here about Unobtrusive Javascript.

Browser other questions tagged

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