cheesegrits
Support Gopher
OK, here's what I did. I moved the bulk of the code into a function, in a form javascript (which go in your ./js folder in the front end fabrik component folder). The name has to be your form ID, in this case 6.js. The content of 6.js is:
Now on your elements 'onchange' javascript, we just need:
I also modified the user_ajax.php function slightly, to look like this:
The main change is the array called $ok_tables. This is a security measure, otherwise people could craft URL's to this routine to basically read any record on any table on your db, which would be a Bad Thing.
If you ever want to read other tables using this function, you'll have to add them to that array.
-- hugh
Code:
function getValues(cid,table,key,label,foreignkey,dropdown)
{
var url = 'index2.php?option=com_fabrik&no_html=1&task=userPluginAjax&method=getFields';
url += '&table=' + table;
url += '&val='+cid;
url += '&key='+key;
url += '&label='+label;
url += '&foreignkey='+foreignkey;
new Ajax(url, {
method: 'get',
update: $(dropdown)
}).request();
}
Now on your elements 'onchange' javascript, we just need:
Code:
getValues(this.getValue(),'joomla_codarchivo2','id','descripcion','codigo','joomla_corresp2008___categor_a_secundaria');
getValues(this.getValue(),'joomla_codarchivo3','id','descripcion','codigo','joomla_corresp2008___3era_categor_a');
I also modified the user_ajax.php function slightly, to look like this:
Code:
class userAjax {
var $ok_tables = array(
'joomla_codarchivo2',
'joomla_codarchivo3'
);
function getFields(){
global $database;
//get all the variables passed in by the ajax objects url
$table = mosGetParam($_REQUEST, 'table');
$key = mosGetParam($_REQUEST, 'key', 'id');
$label = mosGetParam($_REQUEST, 'label', 'label');
$foreignKey = mosGetParam($_REQUEST, 'foreignkey', 'codigo');
$val = mosGetParam($_REQUEST, 'val', '1');
// make sure table is in our allowed list, if not don't do anything.
if (in_array($table,$this->ok_tables))
{
//run a query on the database to get the matching fields
$sql = "SELECT $key AS value, $label AS label FROM $table WHERE $foreignKey = '$val'";
$database->setQuery($sql);
$rows = $database->loadObjectList();
//write the results of the query back to the browser - the javascript code will then assign
//the text to the second drop down
if (!empty($rows))
{
foreach($rows as $row){
echo "<option value=\"$row->value\">$row->label</option>";
}
}
else
{
echo '<option value="0">Nada!</option>';
}
}
else
{
echo '<option value="0">Bad table' . $table . '!</option>';
}
}
}
The main change is the array called $ok_tables. This is a security measure, otherwise people could craft URL's to this routine to basically read any record on any table on your db, which would be a Bad Thing.
If you ever want to read other tables using this function, you'll have to add them to that array.
-- hugh