How to Render images from a image folder path

Thanks for this very valuable post. The demo site shows a very nice effect we all want to show up on our site ! :)

Just a little corner side effect : refreshing the page does not refresh the image. But I guess that's the way image display is triggered (dropdown selection).

Thanks again for taking time to explain all the thing in details.
 
Well, it appears to be almost working. I think there still might be a bug in the image element that is holding it up. I updated to the latest GitHub before posting.

Here is my JavaScript code, which is pasted in the Javascript tab.
Code:
var url = 'index.php?option=com_fabrik&format=raw&view=plugin&task=userAjax&method=getFigure';
var drill_id = this.getValue();
var update = $('mps_score___figure').firstChild;
var myTitle = $('mps_score___title');
new Request({
	url: url,
	data: {
		method: 'getFigure', 'drill_id': drill_id
	},
	onComplete: function (r) {
		update.src = "http://localhost/joomla" + r;
	}
}).send();

Here is the contents of my user_ajax.php
PHP:
<?php
defined('_JEXEC') or die('Restricted access');

class userAjax {
	
	function getFigure() {
		$drill_id = JRequest::getVar('drill_id', "");
		$db = JFactory::getDBO();
			
		$query = "SELECT figure FROM mps_drill WHERE id = $drill_id LIMIT 1";
		$db->setQuery($query);
		$result = $db->loadResult();
		echo $result;
	}
}
?>

No image is displayed in my form. The img src = http://localhost/joomla// for the element. I tried inserting "document.write(update.src);" below the "update.src" statement in the JS and the correct address of "http://localhost/joomla/images/drills/IMG_3583_hZ9.jpg" was displayed.

I did try the line of
Code:
document.getElementById('mps_score___figure').innerHTML = '<img class='imagedisplayor' style='float:none;' alt=''  width='200' height='200'  src=' + r + '>'

That gave a larger broken link icon and the error: GET http://localhost/images/drills/IMG_3583_hZ9.jpg 404 (Not Found). Note the "joomla" portion of the link is missing so that is why it can't be found. There is also a warning: Resource interpreted as Other but transferred with MIME type undefined.
 
If your JS code is more than two lines (on the basis that any code with more than 2 lines in it will have at least one bug, LOL!), I strongly suggest using the XX.js method, as described by felix in a previous post. So find your form's numeric ID (number in far left column of main forms list on the backend), and create ./components/com_fabrik/js/XX.js. In that, put ...

Code:
function myImageUpdate() {
    var url = 'index.php?option=com_fabrik&format=raw&view=plugin&task=userAjax&method=getFigure';
    var drill_id = $('mps_score___drill').getValue();
    var update = $('mps_score___figure').firstChild;
    var myTitle = $('mps_score___title');
    new Request({
    	url: url,
    	data: {
		method: 'getFigure', 'drill_id': drill_id
	},
	onComplete: function (r) {
		update.src = "http://localhost/joomla" + r;
	}
    }).send();
}

NOTE - the only change I've made to this code is this.getValue() becomes $('mps_score___drill').getValue(), because 'this' is no longer in the right scope. You'll need to use your actual element name here, I'm just guessing.

Then in your element JS, instead of all the lines of code, just call ..

Code:
myImageUpdate();

The reason for this is that it makes it a LOT easier to debug. You can now open up FireBug, go to the Script tab, locate XX.js on the Script menu, and put breakpoints in by just clicking in the left margin.

You can then make a change on your dropdown, and you'll be dropped in to the debugger at your breakpoint. From where you can see exactly what is going on, by looking at the Watch window on the right, which shows you all your variable values. You can step through the code line by line. You can put a breakpoint in the onComplete() function which will break when the AJAX response comes back from the server, and see exactly what 'r' is set to. And ... most helpfully, you can switch to the Console window whilst in a break point, and execute any line of code you want, to try it out, without having to modify the script and reload.

-- hugh
 
The reason for this is that it makes it a LOT easier to debug. You can now open up FireBug, go to the Script tab, locate XX.js on the Script menu, and put breakpoints in by just clicking in the left margin.

You can then make a change on your dropdown, and you'll be dropped in to the debugger at your breakpoint. From where you can see exactly what is going on, by looking at the Watch window on the right, which shows you all your variable values. You can step through the code line by line. You can put a breakpoint in the onComplete() function which will break when the AJAX response comes back from the server, and see exactly what 'r' is set to. And ... most helpfully, you can switch to the Console window whilst in a break point, and execute any line of code you want, to try it out, without having to modify the script and reload.
I do actually have a bookmark somewhere where you explained this before. I was hoping to find it and include it in my previous article as to why it's better to go with a x.js form.

Still it keeps your brain active..... :p


I'll port some of this text and my writeup across to the Wiki in the next couple of days.
 
That would be fantastic wiki information. This forum is really useful but it can be difficult to find exactly the right search term in the forum just to bring up pertinent topics. And then you still need to dig through all the posts! Another issue is the subtle differences between versions and enhancements.

I got the external JS setup however I have the same issue (no image displayed). There are no errors or warnings. The output of the JS is the correct image path (http://localhost/joomla/images/drills/IMG_3583_hZ9.jpg) however Firebug shows the image element src = "http://localhost/joomla//" (see attached image). Note the double quotes at the end.

Here is my JS:
Code:
function ajaxFigure(){
	var url = 'index.php?option=com_fabrik&format=raw&view=plugin&task=userAjax&method=getFigure';
	var drill_id = $('mps_score___drill_id').getValue();
	var update = $('mps_score___figure').firstChild;
	new Request({
		url: url,
		data: {
			method: 'getFigure', 'drill_id': drill_id
		},
		onComplete: function (r) {
			update.src = "http://localhost/joomla" + r;
		}
	}).send();
}

Here is my user_ajax PHP:
PHP:
<?php
/* MOS Intruder Alerts */
defined('_JEXEC') or die('Restricted access');

class userAjax {
	
	function getFigure() {
		$drill_id = JRequest::getVar('drill_id', "");
		$db = JFactory::getDBO();
			
		$query = "SELECT `figure` FROM `mps_drill` WHERE `id` = $drill_id LIMIT 1";
		$db->setQuery($query);
		$result = $db->loadResult();
		echo $result;
	}
}
?>
 

Attachments

  • Screenshot at 2011-12-04 14:11:16.png
    Screenshot at 2011-12-04 14:11:16.png
    101 KB · Views: 267
Well I couldn't get it to work without the innerhtml as the image element is different to 2.x.

Incidently, are you running a recent version of Github as there was a problem with the image element that Hugh recently changed. I can't remember if that was on this post or another. I'm too lazy to look at the moment.

.
 
Yes I am updated with the latest GitHub and familiar with the other topic you are referring to.

Thanks for your patience and help. I am really at a loss for why this isn't working for me. I should not have been so quick to assume a bug when you have a working demo.

I believe all the code is working up to actions in the AJAX onComplete statement. I verifed the response using Firefox and also passing it to a display element. I can copy/paste the image path into my browser and it will bring it up. So the question is why this information is not getting passed into my image element.

The first code I tried was what you provided. I changed the path prefix and the name of my image element. No luck. Here are the two lines in question of that I tried.

Code:
update.src = "http://localhost/joomla" + r;
document.getElementById('mps_score___figure').innerHTML = '<img class='imagedisplayor' style='float:none;' alt=''  width='200' height='200'  src=' + r + '>'

I am still confused the update.src and innerHTML statements. As I understand your code, the update.src should set the image attribute "src" to be your modified image path. The innerHTML then sets several other image attributes including "src" again. You mentioned you were just showing a thumbnail of the image versus the full size so is that the reason for all the other stuff? I want to display the full image. So I assume all I need to do is pass an image path to my image element and then it should be displayed? From Hugh's comment in the other topic, it doesn't even need to be a full path if it is in the default images folder.

I would love to just get it working like your demo. Then I could play with it to better understand it. I am wondering if it isn't some other setting or syntax issue that isn't causing an error? Do I need to clear a cache somewhere?
 
What do you have for your trigger? Do you have something like...

ajaxFigure();

...in your onchange?


.
 
Correct. Now that the JS is stored externally, the only thing in the JS tab is the call to the JS function. I also tried changing the trigger to be "on change" or "on load" as well.

I hope to beat my head against this more tonight.:confused: I need to get more familiar with Firebug. I was using the Chrome Developer tools which was fine for errors and CSS. After going to the external JS, I switched to FireFox w/ Firebug as it seems much more capable when it comes to debugging. However Firebug was giving me weird problems that did not seem related to the issue here. Perhaps once I am more familiar with it then it can provide a clue as to what I've been missing so far.

Please let me know if you have any other ideas or comments.
 
OK, got it working with the innerHTML statement. :D Firebug is awesome! I completely removed the .firstChild and update.src statements.

My code was OK; it was my troubleshooting process that was the problem. I need to learn to resist the temptation to change more then one thing at a time. Or better yet, remember what I changed and why.:rolleyes:

I am still desperately curious why the innerHTML statement is used. Yes it works, but like you (felixkat) mentioned in an earlier post, it just seems like there is or should be a better tool for the job.
 
Hmmm... Can you post your final code for both parts?

I did start to play around with this the other day and got side tracked and started modifying the upload element instead on another issue... lol

As I mentioned, the code was ported across from 2.x and I didn't really check what was needed, I just stopped when it started working... ;)

And yes, I need to look into a way of doing it tagging into Fabrik itself. I may hang on until Rob's back so I don't feel so guilty pestering the guys as much.....


.
 
Below is the current code. I am so relieved it is working! :cool: Thanks again for all of your generous help!

A screenshot of my database join element that has the Javascript call is attached.

Here is my external JS:
Code:
function ajaxFigure(){
    var url = 'index.php?option=com_fabrik&format=raw&view=plugin&task=userAjax&method=getFigure';
    var drill_id = $('mps_score___drill_id').getValue();
    new Request({
        url: url,
        data: {
            method: 'getFigure', 'drill_id': drill_id
        },
        onComplete: function (r) {
            var address = "http://localhost/joomla" + r;
            $('mps_score___title').setProperty('text', address);
            document.getElementById('mps_score___figure').innerHTML = '<img class='imagedisplayor' style='float:none;' alt=''  src='+ address +'>'
        }
    }).send();
}
Here is my user_ajax.PHP:
PHP:
<?php
/* MOS Intruder Alerts */
defined('_JEXEC') or die('Restricted access');
class userAjax {
    function getFigure() {
        $drill_id = JRequest::getVar('drill_id', "");
        $db = JFactory::getDBO();
        $query = "SELECT `figure` FROM `mps_drill` WHERE `id` = $drill_id LIMIT 1";
        $db->setQuery($query);
        $result = $db->loadResult();
        echo $result;
    }
}
?>

Should I leave this thread open in case you have a chance to look more into this when things settle down?
 

Attachments

  • drill_id element.png
    drill_id element.png
    89.6 KB · Views: 236
is drill_id always an integer ? if so a more secure version would get it from the request with
PHP:
$drill_id = JRequest::getInt('drill_id');

otherwise you should quote the drill_id to avoid sql injections:

PHP:
$drill_id = JRequest::getVar('drill_id', "");
$db = JFactory::getDBO();
$drill_id = $db->quote($drill_id);
.....
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top