We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

send data as ajax

Hello, I have problems sending data via json. If something look bad me know please. I feel I have error in the foreach

In my controller

public function buscarProductoAction(){
            $sql = "
            select ART_CODIGO, ART_NOMBRE, nvl(ART_costo,0)  as costo,ART_UNIDADC as UNIDAD, NVL(ART_IVA,0) AS IVA from SPM_ARTICULO WHERE ART_ESTADO<>'A'  AND ART_NOMBRE LIKE   "."'%".$_GET['term']."%'";
        $robots = $this->db->fetchOne($sql);
        foreach ($robots as $robot) {
            $return_arr[]=array('id'=>$robot['ART_CODIGO'],'ART_NOMBRE'=>$robot['ART_NOMBRE'],'COSTO'=>$robot['COSTO'],
                                'UNIDAD'=>$robot['UNIDAD'],'IVA'=>$robot['IVA']);
        }
        $this->response->setContentType('application/json', 'UTF-8');
        return $this->response->setJsonContent($return_arr)->send();

    }

In my view

<script>
            $(function() {
            $(".auto").autocomplete({
            source: "buscarProducto",
            minLength: 1,
            select: function(event, data) {
                    $("#codigo").val(data.item.id);
                    $("#iva").val(data.item.IVA);
                    $("#precio").val(data.item.COSTO);
                    $("#unidad").val(data.item.UNIDAD);
                    $("#producto").val(data.item.ART_NOMBRE);
                    $(".auto").val(data.item.ART_NOMBRE);
                    $("#cant").focus();
                    },
                });
        });
</script>

In the jquery i have this problem

jquery.js:9203 GET https://localhost/tesis/spt_encabezado/buscarProducto?term=m 500 (Internal Server Error)

It is ERROR

try {

                    // Do send the request (this may raise an exception)
                    xhr.send( options.hasContent && options.data || null );

I feel I have error in the Loop

edited Jul '16

As you see you have error 500. Check php logs.

$this->response->setJsonContent($return_arr)->send();

You dont need to do send after return response.



81.1k

In the log file

[Sat Jul 02 01:39:41.850838 2016] [:error] [pid 25446] [client 127.0.0.1:28872] PHP Warning:  Illegal string offset 'ART_CODIGO' in         /var/www/html/tesis/app/controllers/SptEncabezadoController.php on line 70, referer: https://localhost/tesis/spt_encabezado/new
[Sat Jul 02 01:39:41.850887 2016] [:error] [pid 25446] [client 127.0.0.1:28872] PHP Stack trace:, referer: https://localhost/tesis/spt_encabezado/new

Is the foreach if it is well done it?

Why you are not using models, binding, and request getQuery ? Why you even using phalcon if you are not using it ?

Read phalcon docs, check any application examples like vokuro etc.



11.6k

not sure cause I do not use this way to fetch result, but maybe here your $robot is an object, not an array, so try to access its properties using $robots->ART_CODIGO (the reported error seems to means that the key you ask for do not exist)



81.1k
edited Jul '16

I made the following changes


     public function buscarProductoAction() {

        if (isset($_GET['term'])) {
               $robots = SpmArticulo::find("ART_NOMBRE LIKE  %'".$_GET['term']."%'");
            foreach ($robots as $robot) {
                $return_arr[] = array('id' => $robot->ART_CODIGO,
                    'ART_NOMBRE' => $robot->ART_NOMBRE,
                    'COSTO' => $robot->ART_COSTO,
                    'UNIDAD' => $robot->ART_UNIDADV, 'IVA' => $robot->ART_IVA);
                $this->response->setContentType('application/json', 'UTF-8');
                return $this->response->setJsonContent($return_arr)->send();
            }
            //var_dump($return_arr);
        }
    }

In the var_dump


    array (size=2)
    0 => 
    array (size=5)
      'id' => string 'ARTIC0000000002' (length=15)
      'ART_NOMBRE' => string 'MEMORIA USB STANDAR 2' (length=21)
      'COSTO' => string '0' (length=1)
      'UNIDAD' => string 'U' (length=1)
      'IVA' => string '0' (length=1)
    1 => 
      array (size=5)
      'id' => string 'ARTIC0000000001' (length=15)
      'ART_NOMBRE' => string 'MAQUINA DE VOTACION' (length=19)
      'COSTO' => string '0' (length=1)
      'UNIDAD' => string 'U' (length=1)
      'IVA' => string '0' (length=1)

IT'Ś OK But when returning only returns a row in the array, the array not all this I checked with the tools of google chrome



11.6k
edited Jul '16

I think you have to declare your $return_arr outside from the foreach loop first; as you do now, a new one is initialized each time, overwritting the previous one

and your ajax rep is inside the loop too, populate your array then send rep only one time



145.0k
Accepted
answer
edited Jul '16

Instead of using

isset($_GET['term'])

use

$this->request->hasQuery('term')
SpmArticulo::find("ART_NOMBRE LIKE  %'".$_GET['term']."%'");

Use conditions, binding and

$this->request->getQuery('term')