details from "database join" to "text fields"

Hello,
I need to get details from selected drop down (database join) to 4 text fields. I think that I need to use javascript and ajax, but I dont know how to do this.

Can you please help me?
 
Hi Rob, thanks for this link, but can you please create an working script for my case?

My table contains:
--------------------
ID
vendor
street
city

database join: key = ID, value = vendor

I need to fill-in text fields called jos_fabrik_formdata_1___to_street (with street value) and jos_fabrik_formdata_1___to_city (with city value).

Here is my javascript (onchange)
Code:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax&method=companyAddress&id';
var id = this.getValue();
var update1 = $('jos_fabrik_formdata_1___to_street');
var update2 = $('jos_fabrik_formdata_1___to_city');
new Ajax(url, {
  data:{
    method:'companyAddress',
    'id':id
  }
,
  onComplete:function(r){
    update.setHTML(r);
  }
}
).request();

user_ajax.php
Code:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
class userAjax {
function companyAddress() {
$db =& JFactory::getDBO();
$myCompanyID = JRequest::getVar('id');
$query = "SELECT street, city from doprava_kontakty WHERE id = '$myCompanyID'";
$db->setQuery($query);
$results = $db->loadResult();
echo $results;
}
}
?>

Firebug: I get in POST correct informations about ID but nothing happens.

PLEASE HELP ME
 
Hi Marcel,
as you want to return more than one value, you'll need an array. I am using JSON, in your case it would be something like:

In user_ajax.php
Code:
$arr = array(0 => $street, 1 => $city);
echo json_encode($arr);
Here's the Javascript you'll need for the element:
Code:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax&method=companyAddress&id';
var id = this.getValue();
var update1 = $('jos_fabrik_formdata_1___to_street');
var update2 = $('jos_fabrik_formdata_1___to_city');
new Ajax(url, {
  data:{
    method:'companyAddress',
    'id':id
  }
,
  onComplete:function(r){
  var arr = JSON.decode(r);

  var street = arr[0];
  var city = arr[1];
  
  update1.set('value', street);
  update2.set('value', city);
  }
}
).request();
Good luck! Please tell me if this works.

Cheers
JJ

Something else I just saw:
I don't know whether that causes problems:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax&method=companyAddress&id';

I am just using
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax';
all the time...
 
Hi JJ,
thanks for fast reply but it doesn?t work.

I get in Firebug this error:
Code:
JSON.decode is not a function
onComplete(Object { name="r"})65 (line 1)
pass()

Once again my scripts:

Javascript
Code:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax&method=companyAddress&id';
var id = this.getValue();
var update1 = $('jos_fabrik_formdata_1___to_street');
var update2 = $('jos_fabrik_formdata_1___to_city');
new Ajax(url, {
  data:{
    method:'companyAddress',
    'id':id
  }
 
,
  onComplete:function(r){
  var arr = JSON.decode(r);
  var street = arr[0];
  var city = arr[1];
 
  update1.set('value', street);
  update2.set('value', city);
  }
 
}
 
).request();

and user_ajax.php
PHP:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
class userAjax {
function companyAddress() {
$db =& JFactory::getDBO();
$myCompanyID = JRequest::getVar('id');
$query = "SELECT street, city from doprava_kontakty WHERE id = $myCompanyID";
$db->setQuery($query);
$results = $db->loadResult();
$arr = array(0 => $street, 1 => $city);
echo json_encode($arr);
}
}
?>
 
Hello again,
of course, you forgot something essential. I thought the database php part was obvious for you.
In your current code the variables $street and $city are empty.
Ok, take this code:

Code:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
class userAjax {
function companyAddress() {
$db =& JFactory::getDBO();
$myCompanyID = JRequest::getVar('id');
$query = "SELECT street, city from doprava_kontakty WHERE id = $myCompanyID";
$db->setQuery($query);
//$results = $db->loadResult();
$results = $db->loadObjectList();

$arr =  array();
foreach($results as $result) {
$arr[0]= $result->street;
$arr[1]= $result->city;
}

echo json_encode($arr);
}
}
?>

Please note that you need loadObjectList();
This code is not perfect, I hope you always get only one row as sql result...
 
Hi,
I get the same error

Code:
JSON.decode is not a function
onComplete(Object { name="r"})65 (line 1)
pass()

I think the problem is in javascript.
 
Hi Marcel,
the only difference in your javascript is the line with the url. Try just using this:
Code:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax';
It will find the function as long as method in js and function in user_ajax.php have the same name.

Then to be sure that it is not the db php part just comment everything except a manual array declaration:
Code:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
class userAjax {
function companyAddress() {
/*
$db =& JFactory::getDBO();
$myCompanyID = JRequest::getVar('id');
$query = "SELECT street, city from doprava_kontakty WHERE id = $myCompanyID";
$db->setQuery($query);
//$results = $db->loadResult();
$results = $db->loadObjectList();

$arr =  array();
foreach($results as $result) {
$arr[0]= $result->street;
$arr[1]= $result->city;
}
*/
$arr = array(0 => "teststreet", 1 => "testcity");
echo json_encode($arr);
}
}
?>

Then have a look in firebug console -> response tab if you get something like
Code:
["teststreet","testcity"]

I do remember this error with "JSON.decode is not a function" but I just cannot remember what caused it.
Are you still using the old Fabrik version? What version of mootools do you use? You can check under fabrik -> tables -> parameters
I am using mootools 1.2 and it is working fine.
I think that's all I can do for you mate. Check that and then wait for some help by some one more experienced than me...

Cheers
JJ
 
Hi JJ again,
really thanks for this replies.

If I comment everything except a manual array declaration as you sent I get the same error in Firebug (including changed URL).

Version of my Fabrik is 1.0.6 with newest SVN files but version of my mootools is 1.1 and 1.11 (found in JS files located in /components/com_fabrik/libs/mootools.js and ...mootools111.js). I have downloaded newest file mootools-1.2.4-core-yc.js but I dont know how to install (use) it. When I copy this file into /components/com_fabrik/libs/ - nothing happens. When I rename it to mootools111.js I get a lot of errors when page is loaded. What files do you have in this folder? Can you please send them to my e-mail MarcelHavlik@seznam.cz?

THANKS, THANKS, THANKS....
 
Hi,
ouuww, be careful. I would never manipulate mootools files. I have absolutely no experience about fabrik v.1 but maybe it is not even ready for mootools 1.2
What hinders you upgrading to fabrik 2RC4???

My mootools folder looks like the screenshot attached. That's how it comes with the fabrik v.2 install.

Think about migrating.

Maybe JSON is also not supported. I remember that I first tried things like concatenating the multiple results into one string, echo that string and cutting it again in the Javascript. Because that ended in errors and frustration I found JSON which works fine.

Regards
JJ
 

Attachments

  • mootools1.2_folder.png
    mootools1.2_folder.png
    12.7 KB · Views: 343
Fabrik 1.0.x won't work with Mootools 1.2, so don't get sidetracked in to that. And JSON.decode() is a Mootools 1.2 thing.

I seem to recall the 1.11 equivalent is Json.evaluate(). Note that the Json is case sensitive.

-- hugh
 
Hi Hugh,
thanks for reply. I have inserted new Json function but I get error from Firebug
Code:
missing ) in parenthetical
   (<?xml version="1.0" encoding="windows.../xhtml1/DTD/xhtml1-transitional.dtd">\n                   mootools111.js (line 263)

Here is my javascript
Code:
var url = 'index.php?option=com_fabrik&format=raw&controller=plugin&c=plugin&task=userAjax';
var id = this.getValue();
var update1 = $('jos_fabrik_formdata_1___to_street');
var update2 = $('jos_fabrik_formdata_1___to_city');
new Ajax(url, {
  data:{
    method:'companyAddress',
    'id':id
  }
,
  onComplete:function(r){
  var arr = Json.evaluate(r);
  var street = arr[0];
  var city = arr[1];
 
  update1.set('value', street);
  update2.set('value', city);
  }
}
).request();

and user_ajax.php
PHP:
<?php
defined( '_JEXEC' ) or die( 'Restricted access' );
 
class userAjax {
   function companyAddress() {
      $db =& JFactory::getDBO();
      $myCompanyID = JRequest::getVar('id');
      $query = "SELECT street, city from doprava_kontakty WHERE id = $myCompanyID";
      $db->setQuery($query);
      $results = $db->loadObjectList();
      $arr = array();
      foreach($results as $result) {
      $arr[0]= $result->street;
      $arr[1]= $result->city;
      }
   echo Json_toString($arr);
   }
}
?>

Thanks for all replies because I am a novice in this scripting.

Regards
Marcel
 
Run FireFox with Firebug installed. Look on the Console tab when your AJAX call fires, and look at the Response section for the call. Copy and paste the output here.

-- hugh
 
I need you to open that user_ajax POST just above the error (click the plus sign on the left) and show me what it has in the Response tab within that call.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top