[SOLVED] Get a value from database using Javascript + PHP

F.schettino

Italian
I use Javascript on blur of an element to get a value from database using a function in user_ajax.php.

Code:
var elemento_Comune = Fabrik.getBlock('form_20').elements.get('fabrik_nominativi___Citta_Residenza');
var Codice_Comune = elemento_Comune.get('value');
var CAP_Comune='';
 
var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=CAP_da_Citta&Codice_Comune=" + Codice_Comune;
 
new Request(
  {url:url,
  method: 'get',
  onComplete: function(r) {alert(Codice_Comune); alert(r)}
}).send();


PHP:
<?php
defined('_JEXEC') or die('Restricted access');
 
 
class UserAjax
{
    public function CAP_da_Citta()
    {
        $db = FabrikWorker::getDbo();
        $query = $db->getQuery(true);
        $CAP_Comune = ' ';
        $app = JFactory::getApplication();
        $input = $app->input;
        $cod_comune = $input->get('Codice_Comune', ' ');
        $query="SELECT CAP FROM fabrik_comuni WHERE Codice_Comune=" . $cod_comune;
        $db->setQuery($query, 1, 0);
        $CAP_Comune = $db->loadResult();
 
        echo $CAP_Comune;
    }
}

It doesn't work; alert(r) is blank.

If I modify last PHP statement from "echo $CAP_Comune;" to
"echo $cod_comune;"​
alert(r) displays:
undefined​
so, the query cannot work.


What is wrong? Why 'Codice_Comune' is not assigned to $cod_comune?

Can someone help me, please?
 
I think that 'codice_cimune' is a string, so try:
$query="SELECT CAP FROM fabrik_comuni WHERE Codice_Comune=" . $db->quote($cod_comune);
 
Thanks for your interest, jfquestiaux .

Probably I was not clear: the problem isn't the query but the input value.

If I try:
PHP:
<?php
defined('_JEXEC') or die('Restricted access');
 
 
class UserAjax
{
    public function CAP_da_Citta()
    {
        $db = FabrikWorker::getDbo();
        $query = $db->getQuery(true);
        $CAP_Comune = ' ';
        $app = JFactory::getApplication();
        $input = $app->input;
        $cod_comune = $input->get('Codice_Comune', ' ');
 
        echo $cod_comune;
 
    }
}

alert(r) displays: undefined.

data 2014.08.26 ora 11h15m16s.png
 
Is it working with just
Code:
<?php
defined('_JEXEC') or die('Restricted access');
 
 
class UserAjax
{
    public function CAP_da_Citta()
    {
 
        echo 'abc';
 
    }
}
?
 
Thanks for your interest, troester .

Surely works; I tried something similar (you can see my #3 Reply).

My problem is pass the input value:
PHP:
class UserAjax
{
    public function CAP_da_Citta()
    {
        $db = FabrikWorker::getDbo();
        $app = JFactory::getApplication();
        $input = $app->input;
        $cod_comune = $input->get('Codice_Comune', ' ');
 
        echo $cod_comune;
 
    }
}


As you see in #1 Replay and #3 Reply, alert(r) displays: undefined.
So $cod_comune is undefined in PHP, that means that Codice_Comune isn't passed to PHP function CAP_da_Citta().
 
This code works well:

Javascript "on change" element
Code:
/* Recupero Codice Comune */
var elemento_Comune = Fabrik.getBlock('form_20').elements.get('fabrik_nominativi___Citta_Residenza');
var Codice_Comune = elemento_Comune.get('value');
var CAP_Comune='';
 
/* Interrogazione del database mediante Ajax per ottenere il CAP del Comune  */
var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=CAP_da_Citta&Codice_Comune=" + Codice_Comune;
 
new Request(
  {url:url,
  method: 'get',
  onComplete: function(r)
    {
      Fabrik.getBlock('form_20').elements.get('fabrik_nominativi___CAP_Residenza').update(r);
      posizione_xx=r.search("xx");
      if (posizione_xx!==-1) {alert("Precisare xx in CAP");}
    }
}).send();


user_ajax.php
PHP:
<?php
defined('_JEXEC') or die('Restricted access');
 
 
class UserAjax
{
    function CAP_da_Citta()
    {
        $db = JFactory::getDBO();
        $cod_comune = JRequest::getVar('Codice_Comune', '');
 
        $query="SELECT CAP FROM fabrik_comuni WHERE Codice_Comune=" . $cod_comune;
        $db->setQuery($query);
        $result= $db->loadResult();
        echo $result;
    }
 
}


I hope that it can be useful for someone. :)
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top