Update data from a while

0

Hello good day My problem is that I have a table of type of vehicles and plates and I need you to update by means of a button, it only works with the first data shown by the while, but it does not change it with the others

MI JAVASCRIPT

function enviarplacas () {


         var tipo_vehiculo;
         var placa;
        datosactualizados2 = {

        tipo_vehiculo : $ ('.vehiculo').val(),
        placa : $ ('.placa').val(),

        idinsp

        };

    $.post('php/formularioactualiza/editar_placas.php', datosactualizados2, function(rtaactualiza2) {
        console.log(rtaactualiza2)     
          if (rtaactualiza2 == 2){
          alert('se ha actualizado correctamente las placas');
          return;
        } 

        if (rtaactualiza2 == 1){
          alert('No se pudo procesar las placas');
          return;
        }   
    });

    console.log(datosactualizados2)
}

MY PHP

<?php

if (isset($_POST['inspeccionID'])    )  {

  $inspeccion_id = $_POST['inspeccionID'];


include("conexion.php");  

   $result = mysqli_query($mysqli," SELECT * FROM vehiculo_inspeccion WHERE  id_inspeccion = '$inspeccion_id' ");


    if (!empty($result)) { 

        if (mysqli_num_rows($result) > 0) {


        while ($fila = mysqli_fetch_array($result)){
               $fila_array['tipo_vehiculo']= $fila['tipo_vehiculo'];
               $fila_array['placa']= $fila['placa'];
               $fila_array['id_veh_ins']= $fila['id_veh_ins'];

echo " 
       <tr>  
         <td colspan='3'><input type='text' class='form-control vehiculo'  disabled='true' id='tipveh".$fila_array['id_veh_ins']."' value='".$fila_array['tipo_vehiculo']."'></td>  
         <td colspan='3'><input type='text' class='form-control placa' disabled='true' id='plc".$fila_array['id_veh_ins']."' value='".$fila_array['placa']."'></td>
         <td colspan='3'><input type='button' class='btn btn-success' id='".$fila_array['id_veh_ins']."' onclick='activarplaca(this)' value='Editar'></button></td>

       </tr>  
    ";      

               } 

        } else { 

echo "
        <tr>  
          <td colspan='6'>  No se encontraron placas en la inspeccion seleccionada. 
          </td> 
        </tr>
     ";     

        }

    } else {

echo "
        <tr>  
          <td colspan='6'> No se encontraron placas en la inspeccion seleccionada. </td> 
        </tr>
    ";


    }
  } else {
    echo "Error";
  }  

?>

MY HTML

<div>
  <center>
<table  width="55%"  rules="all" border="1" align="center" class="table table-bordered table-hover"> 
  <thead bgcolor="#30c151">                                
   <th colspan="3">Tipo vehiculo</th>
    <th colspan="3">Placa</th>
    <th colspan="3">EDITAR</th>
  </thead>

  <tbody class="datosplaca">

  </tbody>
</table>
 <center>
  <button type="button" id="guardardatos" class="btn btn-info" onclick="enviarplacas()"> ACTUALIZAR PLACAS</button>
</center>
</div>
    
asked by JSACTM Music 07.11.2018 в 19:20
source

1 answer

0

Hi mate, the problem in your code is that when you receive the data in your "datosactualizados2" object in the way you are doing it, you will only receive the first value of each of the elements that contain those classes (".vehiculo",".placa") . The simplest thing that occurred to me according to how you generate each line is to group the data for each row generated (tr) ie tr[0](input1,input2,button1),"tr[1](input2,input2,button2)" once I have this you go through with ".each" , then with the method Find. () I look for the elements that I need and I load the values to the object correctly. Sorry if I'm bad explaining. Greetings

function enviarplacas(){
	var datosactualizados2 = [];
	$(".datosplaca tr").each(function() {
		var inputVeh = $(this).find(".vehiculo");
		var inputPla = $(this).find(".placa");
		var inputBtn = $(this).find(":button");
		datosactualizados2.push( {
			 tipo_vehiculo: inputVeh.eq(0).val(),
			 placa: inputPla.eq(0).val(),
			 idinsp: inputBtn.eq(0).attr('id')
		   });
	});
}
// el Resultado seria algo asi 
0: {tipo_vehiculo: "tipo1", placa: "placa1", idinsp: "id1"}
1: {tipo_vehiculo: "tipo2", placa: "placa2", idinsp: "id2"}
    
answered by 08.11.2018 в 01:17