Problems with Ajax in jQuery?

0
  $(buscar_datos);

 function buscar_datos(consulta){

$.ajax({

url:'Ivt.php',
type: 'post',

data:{consulta: consulta},
})
.done(function(respuesta){

    $("#datos").html(respuesta);

})

.fail(function(){

    console.log("error");
})
}

$(document).on('keyup', '#caja_busqueda', function(){

var valor = $(this).val();
//console.log($(this).val())
if(valor != ""){

buscar_datos(valor);
}else{

buscar_datos();
}

});

I have that worse it throws me the error and I do not know what I put wrong

 jquery-1.11.0.min.js:4 POST http://homocervecerus.com/Admin/Ivt.php     500 (Internal Server Error)

that error also gives me

code ivt.php

  <?php
  include '../conexion/server.php';
                          $ot = 0;
                           $to = 0;



             $cons ="SELECT * FROM Productos";

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


                                       //$q = $mysqli ->real_escape_string($_POST['consulta']);

                                       $cons ="SELECT Nombre FROM Productos WHERE Nombre ='.$_POST['consulta'].'";
                                       }

                                       $resultado = $mysli->query($cons);

                                       if($resultado->num_rows >0){

                                        while ($fila = $resultado->fetch_assoc()){

                                            $nom = $fila['Nombre'];
                                            $can = $fila['Cantidad'];
                                            $precun = $fila['Precio'];
                                            $peso = $fila['Peso'];
                                            $ventaca = $fila['CantidadVenats'];
                                            $CodPro = $fila['Codigo'];

                                            $totvent = $fila['cantidadPV'] *$precun;
                                            $totinve = $precun * $can;
                                            $grupo = $fila['Estado'];

                                            if($grupo == 1){
                                                $grupo = 'Embotellado';
                                            }else if($grupo == 2){

                                                $grupo = 'Cocci贸n';

                                                }else if($grupo == 3){

                                                $grupo = 'Instrumentos';

                                                }

                                            echo '<tr>';
                                            echo '<td>'.$fila['Id'].'</td>';
                                            echo '<td><a href="producto.php?id='.$fila['Id'].'">'.$nom.'</a></td>';
                                            echo '<form action="inventario.php" method="post">';
                                            echo '<input type="hidden" value="'.$fila['Id'].'" name="id">';
                                            echo '<td><input type="number" value="'.$can.'" class="la" name="cantidad"></td>';    
                                            echo '<td><input type="number" value="'.$precun.'" class="" style="width:70px;text-align: center" name="precio" step="any"></td>';    
                                            echo '<td><input type="number" value="'.$peso.'" class="" style="width:60px;text-align: center" name="peso"></td>'; 
                                            echo '<td><input type="submit" value="Actualizar" name="act"></td>'; 
                                            echo '<td><input type="submit" value="Borrar" name="borr"></td>'; 
                                            echo '</form>'; 
                                            echo '<td>'.$ventaca.'</td>';  
                                            echo '<td>$ '.number_format($totvent,2).'</td>'; 
                                            echo '<td>$ '.number_format($totinve,2).'</td>'; 
                                            echo '<td>'.$grupo.'</td>'; 
                                            echo '<td>'.$CodPro.'</td>'; 
                                            echo '</tr>';

                                                $ot += $totvent;
                                                 $to += $totinve;

                                                }
                                            echo '<tr>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td></td>';
                                            echo '<td>Total</td>';
                                            echo '<td>$'.number_format($ot,2).'</td>';
                                            echo '<td>$'.number_format($to,2).'</td>';
                                            echo '</tr>';    

                                               if(isset($_POST['act'])){
                                                $ca = $_POST['cantidad'];
                                                $prec = $_POST['precio'];
                                                $pes = $_POST['peso'];
                                                $id = $_POST['id'];

                                                mysqli_query($conect,"UPDATE Productos SET Cantidad = '$ca' WHERE Id='$id'");
                                                mysqli_query($conect,"UPDATE Productos SET Precio = '$prec' WHERE Id='$id'");
                                                mysqli_query($conect,"UPDATE Productos SET peso = '$pes' WHERE Id='$id'");
                                               }  
                                               if(isset($_POST['borr'])){

                    $id = $_POST['id']; 
                                                mysqli_query($conect,"DELETE FROM Productos WHERE Id='$id'");

                                               }  


                           mysqli_close($conect);   


                                       }else {

                                       echo 'yaaaaa';

                                       }



                                       ?>
    
asked by Jose Eduardo 15.02.2018 в 04:09
source

1 answer

0

The way to make a post, more "manageable" would be:

$.post( "Ivt.php",
    {
        act: true,   // DATOS opcionales: que requiere tu servidor
        borr: false, // act,borr,consulta
        consulta: consulta
    }  
 )
.done(function(data) {
    // Esta función se ejecuta cuando no hubieron errores
    // segunda Ejecución
    // data son los datos recibidos del servidor
    alert( "segunda ejecución de exito" + data );
})
.fail(function(err) {
    alert( "error: " + err.mensaje );
});

In the PHP side, try to return the data in JSON format. Here is an example:

$return_arr = Array();
$query = mysql_real_escape_string($_POST['consulta']);
$result = mysql_query("SELECT * FROM Productos");
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    array_push($return_arr, $row);
}
echo json_encode($return_arr);

Documentation obtained from: link link

Try with that code, "Adapt it" to your queries.

    
answered by 15.02.2018 в 05:51