How to do condition drop-down's that link to a database

Status
Not open for further replies.
Oooops!!! You mean I've been walking around with my SVN flies undone all this time??

Thanks for pointing that out. Should be fixed.

-- hugh
 
Well that's simple then - just get all your users to use FF. :)

Point me at the page, I'll see what I can see.

-- hugh
 
I tried the example, but it doesn't pull any info in IE and shows "value\">$row->label" in Mozilla.

The error log shows:

Cannot instantiate non-existent class: userajax in /components/com_fabrik/fabrik.php on line 226

Apache/2.0.52 (Red Hat)
PHP 4.4.7
mySQL 5.0.24
Joomla 1.0.15
Fabrik 1.0- not sure which SVN I'm on Hugh put it in place

It's the daily log, I was working on pulling information from the database to populate the textboxes, but figured I'd try with the country/town example provided to start.
 
Yes (after I read your post).
geek-monkey.jpg


Works in Mozilla, not in IE. Here's the user_ajax.php
Code:
[SIZE=2]
/* MOS Intruder Alerts */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
class userAjax {
function userExists() {
global $database;
$retStr = '';
$myUsername = mosGetParam($_REQUEST, 'username', '');
$query = "SELECT name from #__users WHERE username = '$myUsername' LIMIT 1";
$database->setQuery($query);
$results = $database->loadObjectList();
if ($thisName = $results[0]->name) {
$retStr = "The username $myUsername is already in use by $thisName";
}
echo $retStr;
}
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', 'country_id');
$val = mosGetParam($_REQUEST, 'val', '1');

//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
foreach($rows as $row){
echo "<option value=\"$row->value\">$row->label</option>";
}
}
}
?>
[/SIZE]

Thanks,
Dave
 
OK, I'll post some corrections to the code which let it work in IE. The problem being, IE doesn't let you just replace the options in a select, you have to rebuild the entire select statement. I have a working version somewhere, I'll post it as soon as I find it.

-- hugh
 
Can you please see what I might be doing wrong.

I have form connected to a table "jos_fabirk_formdata_4"

table one is called "process_category" in fabrik attached to "jos_process_category"
with elements fields: "id" and "title"

the second is called "process_group" in fabrik attached to "jos_process_group" in the database with element feilds: "id", "parent_id" and "title"

I create the first element on the form called "process_category"
Type: "database join"
table: "jos_process_category"
key: "id"
lable: "title"
added Java code:
****************************************************
var cid = this.getValue();
var table = 'jos_process_categories';
var key = 'id';
var label = 'title';
var foreignkey = 'parent_id';
var dropdown = 'jos_fabrik_formdata_4___process_groups';
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();
***************************************************
I create the second element on the form called "process_group"
type: "dropdown"
value: [I leave empty]
label: "Please Select..."

I then create the user_ajax.php file with the following content:
***************************************************
/* MOS Intruder Alerts */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class userAjax {

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', 'title');
$foreignKey = mosGetParam($_REQUEST, 'foreignkey', 'parent_id');
$val = mosGetParam($_REQUEST, 'val', '1');

//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
foreach($rows as $row){
echo "<option value=\"$row->value\">$row->label</option>";
}
}
}
***********************************************

Can you see where I might be going wrong?

Thank you
 
URL?

Are you using FireFox with FireBug? If not ... load it up. FireBug will tell you exactly what is being returned from the AJAX call (under the Net or Console tabs), which is often enough to tell you what the problem is.

-- hugh
 
please help

i?ve been trying it for many days, but there is no chance... the first dropdown displays the countries and the second dropdown doesn?t change.. nothing happens? what?s wrong?

i think i did everything described in the thread...

the tables are named: countries & towns
the table with the dropdowns is called: jos_fabrik_formdata_5
the 1st dropdown element?s name is country - the 2nd: town

country-table: jos_fabrik_formdata_9
town-table: jos_fabrik_formdata_10

my elements in the table countries:
name: time_date.........................label: time_date
name: fabrik_internal_id................label: id
name: id....................................label: id
name: label................................label: label

my elements in the table towns:
name: time_date.........................label: time_date
name: fabrik_internal_id................label: id
name: id....................................label: id
name: country_id........................label: country_id
name: label................................label: label

is this correct?

this is my actionskript-code:

//get the country id that has been selected by the user
var cid = this.getValue();

// this is the database table name that contains the information you want to display in the town drop down
var table = 'jos_fabrik_formdata_10';

//these are the fields in the 'towns' table
var key = 'id';
var label = 'label';
var foreignkey = 'country_id';


// this is the html id of the drop down element we want to update
var dropdown = 'jos_fabrik_formdata_5___town';

//below here you shouldnt need to edit anything

//we want to create an ajax object to send a request to fabrik's userPluginAjax class - this call contains the variables
//outlined above and update the second drop down with the data returned from the server

var url = 'index2.php?option=com_fabrik&no_html=1&task=userP luginAjax&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();


and this is my user_ajax.php:

<?php

/**
* @package fabrik
* @version 905 | fabrik_cheesegrits | 2008-05-19 21:21:05 +0200 (Mon, 19 May 2008
* @Copyright (C) Rob Clayburn
* @license GNU/GPL http://www.gnu.org/copyleft/gpl.html
*/

/**
* This is an example file. To use userAjax, copy this file to user_ajax.php,
* and insert your function into the userAjax class, as per the example
* userExists() function. To call your AJAX method, use a URL of this format from
* your custom JS code:
*
* index2.php?option=com_fabrik&no_html=1&task=userPl uginAjax&method=userExists&username=" + myUsername;
*
* Fabrik will automatically try and call the function name specified in your 'method='.
* You are responsible for grabbing any other parameters, using mosGetParams,
* as per the $myUsername exmaple in userExists() below.
*
* The userExists() example is designed to test if a username given in a text element
* exists. If it does, an alert will pop up, then the field will be cleared and the cursor re-focused to it.
*
* The easiest way to call AJAX from your JS is to use the Mootools Ajax class, for instance:
*
* function userExists(myUsername,refocus) {
* var url = "index2.php?option=com_fabrik&no_html=1&task=u serP luginAjax&method=userExists&username=" + myUsername;
* new Ajax(url, {
* onComplete: function(response){
* if (response != '') {
* alert(response);
* refocus.value = '';
* refocus.focus();
* }
* }
* }).request();
*}
*
* In this case, the above code is called from the 'onchange' trigger
* of a text element like this:
*
* var thisElement = $('jos_fabrik_formdata_13___username');
* var myUsername = thisElement.value;
* userExists(myUsername,thisElement);
*
* Note that there may be better ways of doing this, the above is just the way I found
* to get it done. The element JS grabs the content of the text field, and also supplies
* the element object, so the userExists() function can then empty and refocus if the
* specified username already exists.
*
* Another example of using Mootools Ajax might be something like this, which assumes a function
* in this file called buildStateDropDown() (not shown here), which would build the dropdown
* menu for a list of states which you want to update on the fly (for instance if you
* have a "Country" dropdown, and wish to repopulate the State menu when it changes):
*
* function ajaxTest() {
* var url = "index2.php?option=com_fabrik&no_html=1&task=u serP luginAjax&method=getStateDropDown";
* new Ajax(url, {
* method: 'get',
* update: $('jos_fabrik_formdata_13___states')
* }).request();
* }
*
* The important note here is the 'update' parameter, which tells Mootools the ID of the
* form element you want to replace with the AJAX response.
*
*/

/* MOS Intruder Alerts */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

class userAjax {

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', 'country_id');
$val = mosGetParam($_REQUEST, 'val', '1');

//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
foreach($rows as $row){
echo "<option value=\"$row->value\">$row->label</option>";
}
}
}

please, please help me!!! firebug is already running but i?m not sure to understand it?! how can i see the problem?

hope 2 hear from u soon!
 
Can you point me at the page?

You'll probably need to bump this thread in a day or two. If you check New Posts you'll see that Rob and I are having a 2.0 Bug Squishing Extravanganza for a day or two.

-- hugh
 
Try removing all the comment lines (the ones that start with //) from your JavaScript. There's some discussion of that earlier in this thread.

-- hugh
 
Last question: do you have any solution to fix the problem with displaying it in IE? It?s working fine in Firefox...
 
Yeah, it requires changing the way we rebuild the SELECT, because IE won't let you just change the innerHTML like FF does. You have to rebuild the SELECT from scratch.

I'll have to update the initial post in this thread.

-- hugh
 
Yeah, it requires changing the way we rebuild the SELECT, because IE won't let you just change the innerHTML like FF does. You have to rebuild the SELECT from scratch.

I'll have to update the initial post in this thread.

-- hugh

YOU 'da man.' :)
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top