Ajax update only on selected line

Asked

Viewed 306 times

0

I have a Datatable here and I’m using a plugin so that the user can edit the table data, got in parts(haha), the problem is that my call is not identifying what the user enters, and when I manually set the cell value it changes in all rows of that column in db, I imagine I need to identify the row ID the user is editing but I don’t know how to do.

Below my Ajax Call:

    $.ajax({
        url: "include/edita.php",
        type: "POST",
        data: { 'coletadora': 'Definição Manual'},                   
        success: function(data){
            alert(data);                                    
        }
    });

It is also relevant the content of my edita.php (ignore the defamed php, was long away from php and need this system running before transitioning to PDO), I know I need a WHERE to limit the update to this particular ID that we will discover:

    <?php
$host= 'localhost';
$bd= 'minhadb';
$userbd = 'meuusr';
$senhabd= 'meupw';



//Conectando com banco MySQL
$conexao = mysql_connect($host,$userbd,$senhabd);
if (!$conexao)
    die ("Erro de conexão com localhost, o seguinte erro ocorreu -> ".mysql_error());
//Conectando com a tabela do banco de dados
$banco = mysql_select_db($bd,$conexao);
if (!$banco)
    die ("Erro de conexão com banco de dados, o seguinte erro ocorreu -> ".mysql_error());
$coletadora   = $_POST["coletadora"];
$sql = "UPDATE minhadb SET coletadora = '$coletadora'";

mysql_query($sql,$conexao);
?>

Thanks in advance for the help!

1 answer

0

It is why you have not informed, through the key Primary or other unique value, which line should be changed in the database.

An output for this can be the inclusion of the changed line id in the request.

For example: (Taking into account that you use jQuery)

<td data-id="1" data-coluna="coletadora">VALOR DA CÉLULA</td>

When your user submits the changes, you should include both the column and the new value and the "identifier" of that row, which in this example would be the id.

Ex:

$.ajax({
        url: "include/edita.php",
        type: "POST",
        data: { 
                $('tdQueOUserAlterou').data('coluna') : 
                $('tdQueOUserAlterou').text(),
                'id' : $('tdQueOUserAlterou').data('id')
        },                   
        success: function(data){
            alert(data);                                    
        }
    });

Then the SQL query could be changed to:

$id = $_POST['id'];
$sql = "UPDATE minhadb SET coletadora = '$coletadora' WHERE 'id'='$id'";

This WHERE tag is what defines which line to change. You want to set the collector equal to such a value ONLY where the id is equal to that entered. Remembering that it can be any single field in your table and not only id.

  • The logic of the process I had in mind, the difficulty in applying is in the fact that this Datatable is generated from a SELECT in my db, so I can not hit the syntax of jquery to capture the ID of the line I am editing. What can help me is the fact that the line id is shown as "Protocol" for the user to query after scheduling, it was a shortcut I took, but I do not know how to use this in my favor, if it is necessary.

  • Take a look at how the table @victormoraesgs : http://pasted.co/ed579cd4

  • @Fernandogross You can place $Row['id'] as a property of every td of that line.

  • @Fernandogross another possibility that I thought about now, is to give a name to each <td> with its respective column, <td name="id">$row['id']</td>, this makes it possible for you to select that cell from the edited cell: $('tdEdidata').siblings('td[name="id"]').text(); this would bring the id of that line to your AJAX request.

  • Opa, in your first example I had the idea to do after your first answer yesterday, I just did not know how Ajax is so that the user can change the cells Collector, Date Collection and Time Collection only for the id of the Row that he selected, I think that’s all that’s missing. Already in your code the problem was that it was all out of order the table, not respecting the order of th, with some study can order,

  • Code of the amendment I had made: http://pasted.co/66b90f36

  • @Fernandogross The only problem with your example is how you named the tags, for example: <td id="collector".... The id of an element must be unique, so you cannot use the collecting id for several different cells (since it has several lines). Answer me this, how will you edit your chart? There is a centralized Save button that saves all lines or a save button per line?

  • Look at the Ajax call as it is now: http://pasted.co/43edc2f8 It sends everything as you can see.

  • The moment the user clicks on the cell he wants to edit, he opens a field to enter the new value, with a button to confirm and another to cancel, he can click on more than one cell at the same time (another field opens to each editable cell clicked), as we could see in Firebug even though he click "confirm" in a single cell it sends the data of all cells that I determined editable, I imagine that if the user did not change a cell that opened it sends the current cell data.

Show 5 more comments

Browser other questions tagged

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