Load data in a table without ajax

1

This is the first question I ask in this medium, I am new in terms of WEB programming and I am costing certain things.

I have a problem when loading data, to see if I can explain myself:

  • I'm using php and javascrip for a university project is about a construction company.
  • For this ALTAS form, I need to load the services offered by this construction company and indicate which materials are used when doing this service, as well as the amount to be used for each meter of this service.
  • When creating a new service, I must indicate the materials to be used, these already exist in the database
  • By clicking OK, I need that material entered to be shown in a table below. and so on until loading all the materials that will be used.
  • Only at the end of the loading of materials to be used
  • I know that this can be done with ajax, but I do not have any knowledge of ajax and it does not give me the time anymore. If you have a small example with ajax, I think it would be useful.

I thank everyone for the help. regards

    
asked by Diego Arce 09.10.2018 в 21:58
source

1 answer

3

We must first clarify if what you need is that the data be shown in the table below or if you need the data to be shown in the table below and stored in the base of data .

If it is the first, you can make a script that takes the values of the form, and thereby dynamically create a new row in the table below.

document.getElementById('botonOk').addEventListener('click', agregarFila);
(function(){
  // Obtener todos los elementos boton-eliminar
  // que ya están cargados
  let botones = document.querySelectorAll('.boton-eliminar');
  
  // Agregar listener a todos los botones
  botones.forEach(function(e){
    e.addEventListener('click', eliminarFila);
  });
})();

function agregarFila(event){
  event.preventDefault();

  let campo1 = document.createElement('td');
  let campo2 = document.createElement('td');
  let campo3 = document.createElement('td');
  
  // Crear campo botón con un botón adentro
  let campoEliminar = document.createElement('td');
  let botonEliminar = document.createElement('button');
  
  botonEliminar.type = 'button';
  botonEliminar.innerText = 'Eliminar';
  botonEliminar.className='boton-eliminar';
  // Agregar función listener al botón
  botonEliminar.addEventListener('click', eliminarFila);
  
  campo1.innerText = document.getElementById('campo1').value;
  campo2.innerText = document.getElementById('campo2').value;
  campo3.innerText = document.getElementById('campo3').value;
  
  campoEliminar.appendChild(botonEliminar);
  
  let nuevaFila = document.createElement('tr');
  nuevaFila.appendChild(campo1);
  nuevaFila.appendChild(campo2);
  nuevaFila.appendChild(campo3);
  nuevaFila.appendChild(campoEliminar);
  
  document.getElementById('tablaDestino').querySelector('tbody').appendChild(nuevaFila);
}

function eliminarFila(event){
  // Obtener nodo fila que contiene el botón
  let fila = this.parentElement.parentElement;
  fila.remove();
}
<form action="#">
    <input type="text" name="campo1" id="campo1" />
    <input type="text" name="campo2" id="campo2" />
    <input type="text" name="campo3" id="campo3" />
    <button type="button" id="botonOk">Agregar</button>
</form>
<table id="tablaDestino">
    <thead>
        <th>Campo 1</th>
        <th>Campo 2</th>
        <th>Campo 3</th>
        <th>Eliminar</th>
    </thead>
    <tbody>
        <tr>
            <td>Precargado 1</td>
            <td>123456</td>
            <td>123456</td>
            <td><button type="button" class="boton-eliminar">Eliminar</button>
        </tr>
    </tbody>
</table>

If you need to save it in the database, you can use the same method to display the data, and at the end of the function agregarFila make an asynchronous call (AJAX) to insert the record. It should be mentioned that the validations necessary to insert the data (data integrity, non-empty fields, etc.) are not considered in the snippet.

    
answered by 09.10.2018 / 23:13
source