Searcher in JQuery, Ajax and PHP (Error)

0

I try to create a search form in the following way:

  • By default, the order history appears:
$(document).ready(function(){
  $.ajax({
    type: 'POST',
    url: 'php/cargar_historial.php'
  })
  .done(function(listas_rep){
    $('#historial').html(listas_rep)
  })
  .fail(function(){
    alert('Hubo un errror al cargar el historial de pedidos')
  })
})

Even here, all right, it shows me by default the history without problems. Now, on top of the table, I added a "form" with 4 fields to "search" in the history.

<form..
    <th><input type="text" id="NPedido" placeholder="N Pedido"></th>
    <th><input type="text" id="Fecha" placeholder="Fecha"></td>
    <th><input type="text" id="Codigo" placeholder="Codigo"></th>
    <th><input type="text" id="Cliente" placeholder="Cliente"></th>
    <input style="width: 100%;" type="submit" id="submit" value="Buscar">
</form>

When I try to search with this code.

JQuery:

$(document).ready(function(){
  $("#buscar_pedidos").submit(function(){

    var id_pedido = $('#NPedido').val()

    $.ajax({
      type: 'POST',
      url: 'php/buscar_npedido.php',
      data: {'id': id_pedido}
    })
    .done(function(listas_rep){
      $("#historial").empty();
      $('#historial').html(listas_rep)
    })
    .fail(function(){
      alert('Hubo un errror al cargar los datos')
    })

  })
})

PHP:

<?php 
require_once 'conexion.php';

function getPedidosN(){

  $mysqli = getConn();
  $id = $_POST['id'];
  $query = "SELECT * FROM 'historial' WHERE npedido == $id";
  $result = $mysqli->query($query);

    $indices = '<table>
                 <thead>
                    <tr>
                        <th>NumP</th>
                        <th>Fecha</td>
                        <th>Codigo</th>
                        <th>Cliente</th>
                        <th>Referencia</th>
                        <th>Detalles</th>
                        <th>Lente</th>
                    </tr>
                 </thead>';

  while($row = $result->fetch_array(MYSQLI_ASSOC)){

    $indices .= '<tbody>
                    <tr>
                        <td>' .$row[npedido]. '</td>
                        <td>' .$row[fecha]. '</td>
                        <td>' .$row[cod_cliente]. '</td>
                        <td>' .$row[nombre_cliente]. '</td>
                        <td>' .$row[referencia]. '</td>
                        <td>' .$row[detalles]. '</td>
                        <td>' .$row[tipo]. '</td>
                    </tr>
                 </tbody>';
  }

  $indices .= '</table>';
  return $indices;
}

echo getPedidosN();
    
asked by Javier Avila Fernandez 28.08.2018 в 16:38
source

2 answers

1

Your problem is with the reload of the page when doing submit. You have to modify the jquery of the onsubmit.

$(document).ready(function(){
  $("#buscar_pedidos").submit(function(e){ // Modifica esta línea
    e.preventDefault(); // Te falta esta línea
    var id_pedido = $('#NPedido').val()

    $.ajax({
      type: 'POST',
      url: 'php/buscar_npedido.php',
      data: {'id': id_pedido}
    })
    .done(function(listas_rep){
      $("#historial").empty();
      $('#historial').html(listas_rep)
    })
    .fail(function(){
      alert('Hubo un errror al cargar los datos')
    })

  })
})
    
answered by 28.08.2018 / 16:58
source
0

you could place the error log, or images. Where is the error generated? in the request, answer, on the server?

Try changing this line: $query = "SELECT * FROM historial WHERE npedido == $ id "; by: $query = "SELECT * FROM historial WHERE npedido = $ id ";

    
answered by 28.08.2018 в 16:46