We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 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) {
        $this->response->setContentType('application/json', 'UTF-8');
        return $this->response->setJsonContent($return_arr)->send();


In my view

            $(function() {
            source: "buscarProducto",
            minLength: 1,
            select: function(event, data) {

In the jquery i have this problem

jquery.js:9203 GET https://localhost/tesis/spt_encabezado/buscarProducto?term=m 500 (Internal Server 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.


You dont need to do send after return response.


In the log file

[Sat Jul 02 01:39:41.850838 2016] [:error] [pid 25446] [client] 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] 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.


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)

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();

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

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

edited Jul '16

Instead of using



SpmArticulo::find("ART_NOMBRE LIKE  %'".$_GET['term']."%'");

Use conditions, binding and