Delete order's details' line without changing page using AJAX
PHP

Variable names, excess variables, logic

When iterating through the rows in showOrderDetails(), the variable $row_index is slightly misleading because that should contain the column name, not an index (typically an integer value):


foreach ($table_row as $row_index =&gt; $row_data) { // titulos campos



A better name for that key would be something like $column_name:

foreach ($table_row as $column_name =&gt; $column_value) { // titulos campos


But the name $row_index would be appropriate for the key on the outer foreach:

foreach ($res as $row_index =&gt; $table_row) {   


Then instead of creating an extra variable called $first, you could utilize $row_index and $column_name to conditionally display the rows and cells.

The first one:


if($first){ //HEADERS



could then be updated to: 

if($row_index === 0){ //HEADERS


And then the second one, in the nested loop:


if($first){ //TO NOT DISPLAY ID



Could be changed to:

if($column_name === 'id'){ //TO NOT DISPLAY ID


And the second inner loop could be updated as well:


foreach ($table_row as $row_data) { // DATA
      if ($first){
          continue;



Could be rewritten to add the row if the column name is id: 

foreach ($table_row as $column_name =&gt; $column_value) { // DATA
    if ($column_name === 'id'){
        $idLinea=$column_value;


And then $first could be eliminated entirely.

Alternatively, the condition in the first nested loop could be negated and then the continue could be eliminated:

if($column_name !== 'id'){ //DO NOT DISPLAY ID
    echo "&lt;th&gt;$column_name&lt;/th&gt;";
}


See that all together in this playground example (not connected with Javascript/AJAX requests).

JS

The line to look up the element to remove by id attribute:


var fila=document.querySelector('#fila'+idLinea); //We choose the first and unique row which has whatever ID needed.



Could be changed to use document.getElementById()

  var fila=document.getElementById('fila'+idLinea); //We choose the first and unique row which has whatever ID needed.  


There are a few discussions about which to use: 


this Sitepoint forum
this SO question and its answers (and related posts)


The general consensus is that getElementById() is quicker than querySelector(). For a small page like this it would likely not be noticeable.