JQuery Array in prestashop

0

I've been stuck with a prestashop script for a couple of days. I need to bring some values from the database and I'm doing them using jQuery, since those values depend on what a user has selected on the web. The problem is that I'm not doing it right, or then I can not access the values that the array brings.

I tried to update the call and now it gives me an error in console (TypeError: invalid 'in' operand a)

This is the call to the jquery:

<script languaje="JavaScript" type="text/javascript">
/*Comprobamos si tiene localidad ya seleccionada*/
    function buscarProveedor()
    {
        var localidad=document.getElementById("localidad").value;
        var idLocalidad=document.getElementById("id_localidad").value;
        if(localidad)
        {
            /*Traemos todos los proveedores de esa localidad*/          
            document.getElementById("nombrelocalidad").innerHTML = localidad;
            var params = {
                "id_localidad" : idLocalidad
            };
            console.log('Aquí si entra');
            $.ajax({
                data: params,
                url: 'TraerProveedoresPorId.php',
                type:'post',                                                
                success: function(response){
                    $.each(response, function(index, data){                                         
                        $("#proveedor").append("<option value=" + data.name + ">" + data.name + "</option>");
                    });
                }           
            });
        }
        else
        {
            alert("Debe seleccionar antes una localidad.");
        }
    }

    /*Cuando esté todo cargado ejecutamos la funcion que trae la localidad del producto*/
    window.onload = buscarProveedor();  
</script>

And now the php that receives the jquery makes the query:

<?php
    include '../config/config.inc.php';


    $variable = Tools::getValue('id_localidad');
    $query = "SELECT name FROM ps_supplier WHERE id_localidad ='" . $variable ." '";
    $id = Db::getInstance()->executeS($query);      
    echo $id;
?>

In the console log this error appears:

Thank you very much, compañeros.

    
asked by jandresplp 28.11.2016 в 13:42
source

3 answers

0

In the end I managed to find the solution after investigating.

<script languaje="JavaScript" type="text/javascript">
/*Comprobamos si tiene localidad ya seleccionada*/
    function buscarProveedor()
    {
        var localidad=document.getElementById("localidad").value;
        var idLocalidad=document.getElementById("id_localidad").value;
        if(localidad)
        {
            /*Traemos todos los proveedores de esa localidad*/          
            document.getElementById("nombrelocalidad").innerHTML = localidad;
            var params = {
                "id_localidad" : idLocalidad
            };          
            $.ajax({
                data: params,
                url: 'TraerProveedoresPorId.php',
                type:'post',
                dataType:'json',
                success: function(response){
                    $.each(response, function(index, data){                     
                        $("#proveedor").append("<option value=" + data.id_supplier + ">" + data.name + "</option>");
                    })
                }                       
            });
        }
        else
        {
            alert("Debe seleccionar antes una localidad.");
        }
    }


    /*Cuando esté todo cargado ejecutamos la funcion que trae la localidad del producto*/
    window.onload = buscarProveedor(), asignarProveedor();

</script>

And the php that jQuery calls me stays like this:

<?php
    include '../config/config.inc.php';


    $variable = Tools::getValue('id_localidad');
    $query = "SELECT id_supplier, name FROM ps_supplier WHERE id_localidad ='" . $variable . "'";   
    $id = Db::getInstance()->executeS($query);      
    echo json_encode($id);

?>

I hope it can help someone. Regards greetings.

    
answered by 30.11.2016 / 18:04
source
1

Friend your problem is definitely in javascript. However there are several recommendations that could make you how Prestashop works to optimize your code:

  • It is only necessary to include this file: include '../config/config.inc.php';
  • The values of the variables passed by GET and POST are obtained with the function: Tools::getValue('id_localidad')
  • You can remove: p($id);
  • Look in the original Prestashop code for some ajax call with jquery so you can see how they process the results.

    Good luck.

        
    answered by 28.11.2016 в 16:46
    1

    Try to put on the success

    for(var i=0;i<response.length;i++){
        var element=response[i]
        //for(var key in element){
           console.log(element);
        //}
    }
    

    Let's see if it shows you the data.

        
    answered by 28.11.2016 в 16:40