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

Status
Not open for further replies.
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:

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
 
Terp said:
Is it possible to 'condition' text fields, per se? I am not sure what you call it, but have seen some forms that I would describe as ajax conditioning, for lack of proper jargon. ;)

When you start to type text in the field, a list of options already in the db appear under the text field, allowing you to click the displayed option and then, in turn, populating the field; a good example is on http://www.kayak.com/ In the "To" field, start typing a city or state or something and you'll see what I mean. Is this a form of "conditioning?" :) Looks like some fancy onkeypress trigger, but not sure how they get the data below the respective field. ;)

How about this attempt to hijack the thread? ;) Would this be considered 'contitioning' and would it be possible with Fab and Ajax? I would populate a column with all the possible selections for the field, then when they hit a key, filter on the fly? I am not sure what they are doing, but cool nonetheless! :)
 
That kind of thing can be done with Ajax, but you really need to have a super fast server on some good bandwidth to make it usable, otherwise it just becomes an annoyance. Basically you need to be able to fire off the Ajax call to the server and get the response back within a few tens of milleseconds. Not many standard hosting packages will get you that kind of snappy response.

Most of the time, people will build large content arrays in Javascript on the initial page load, and select from those after you've typed 3 letters or so, rather than using Ajax. So no communication between browser and host for the 'conditioning'.

-- hugh
 
Hugh Messenger said:
That kind of thing can be done with Ajax, but you really need to have a super fast server on some good bandwidth to make it usable, otherwise it just becomes an annoyance. Basically you need to be able to fire off the Ajax call to the server and get the response back within a few tens of milleseconds. Not many standard hosting packages will get you that kind of snappy response.

Most of the time, people will build large content arrays in Javascript on the initial page load, and select from those after you've typed 3 letters or so, rather than using Ajax. So no communication between browser and host for the 'conditioning'.

-- hugh

Thanks. Yea, the latter is how I thought it would work...have it all load ahead of time (like tab modules that tab between hidden and displayed content when you click, though all tabs are loaded with the page initially compiles).

Does this technique have a proper nomenclature? :) I will do some research, find some examples, and try to grab some code out there during my playing around stage. :)
 
I don't know of any specific nomenclature.

Your best bet is to clone a template, and modify it so you can add the PHP at the top to grab what you need from the database, then build some JS on the fly using that data to build the array(s) and functions you call from your element JS actions.

-- hugh
 
Hello,
Although everything is fine with the drop-downs, there's a detail that I must be overlooking somewhere. When I open my form, I choose the options that load the other drop down options, everything goes well, and then I save the record. After that, in the table view, only the first drop down shows the correct option, the other drops downs keep showing "Please select...", instead of the selected option. If I go to edit again, only the first drop down shows the selected option. Why is this? I think it might have something to do with what I have in the drop downs value, which is currently set to ''.
Thank you for your help.
Herta
 
I might be stupid but i can't make it work, nothing happen. Please take a look at my information

table 1: jos_fabrik_formdata_27
fields: fabrik_internal_id, time_date, regiune

table 2: jos_fabrik_formdata_28
fields: fabrik_internal_id, time_date, judet, id_regiune (foreign key to hold first table id)

My javascript was added in jos_fabrik_formdata_27.regiune element in javascript area:

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

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


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

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

Here is my user_ajax.php function:

Code:
/* 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', 'id_regiune');
		$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 help
Thank you
 
Hi,
Any suggestions on my last post? Please?? The options selected in the drop downs (that link to other tables) can't be seen in the table view after saving, the "Please select category" keeps showing instead of the selected option although it's being correctly saved into the database, it just doesn't show in the fabrik table view or when editing the same record.

Thank you!!
 
You missed the first few lines from Rob's example:

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

-- hugh
 
Thanks for your answer, I will check that but there's still the same problem with the database join drop downs, the selected option can't be seen when viewing only the record.

Another thing I have realized is that these drop downs dont work in IE, they do in Firefox, I dont know about other browsers. This info might be useful for others. I was going crazy trying to figure out if I had messed up something when it was that I had changed computers with different web browsers.

Thanks.
 
I'm sorry, I don't know where should I put this code since you modified my code and it's not exactly the same as Rob's example:

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

I've already tried placing it in several places but I get the same result. I would really appreciate a hint...

I have another question, the database is recording the id (i.e 1), not the descriptive data of these dropdowns, how should I do to have the actual option selected in the drop down be recorded in the database instead of its id number?

Thanks again and sorry...your application is not easy, at least not for us ignorants in sql and javascript...but its versatility is exactly what I need for what I want to accomplish, I really wish I would know more so I could advance faster and not bother you anymore! Sorry...
 
Thanks for your answer, I will check that but there's still the same problem with the database join drop downs, the selected option can't be seen when viewing only the record.

Another thing I have realized is that these drop downs dont work in IE, they do in Firefox, I dont know about other browsers. This info might be useful for others. I was going crazy trying to figure out if I had messed up something when it was that I had changed computers with different web browsers.

Thanks.

I have the same problem.. the dropdowns both work fine in Firefox but it doesn't work in IE!
Anybody has a solution for this nasty problem? ;-)
 
The SVN code is our 'latest and greatest' version of the code. The ZIP's are built from 'snapshots' of SVN whenever we decide to put out a new ZIP version. So the SVN code is always "the last ZIP release plus all bug fixes and new features added since then".

So basically, when you think you have found a bug, I need you to upgrade to the latest SVN, so we can establish if it has been fixed since the last realease. We commit fixes and improvements to 1.0.x almost every day, so there's a good chance it has been fixed in SVN.

So ... upgrade to the latest SVN as per the link in my sig. Remember, DO NOT uninstall Fabrik, just upload the files from SVN, overwriting your existing Fabrik files. And make sure you upload everything (front and backend files).

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top