[Pro] Keyword Help?

I’m trying to code a web page to display photo names, URLs, and keywords (in editable text fields) so I can more easily add keywords that can be searched on the web (from a MySQL DB). This much I’ve accomplished.

My issue is that I can’t find a javascript that will allow me to take a keyword entered in a field and then add it to the text fields attached to each photo I click on the page.

I’ve wandered the web in search of such a script, but have been unsuccessful. Does anyone know of any such javascript, or have any insights into how to write one? I’m a complete noob at javascript.

Thanks for any help any of you can provide. I realize that this isn’t a javascript forum, but it’s a much friendlier place than most dedicated JS forums, so it’s to you that I’m first posing my query.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If you can show us a link to your page layout, with a caption or something showing where you want to click and what you want to be sent where, that would make this so much easier.

What you are describing sounds simple enough, it will only be a matter of getting Freeway to put IDs on things so you can “talk” to them with either Prototype.js’s $() function or the long-hand equivalent in normal JavaScript of getElementById().

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

waltd:

I realized after I wrote the initial post here that I was being a bit vague, but it was late. Sorry.

Here’s the URL of the page that does the keywording:

http://crnich.dyndns.org/familyphotos/php/imageupdater2.php

I’m not doing this in Freeway, but rather hand-coding it, as the page layout is very simple and is entirely generated by the PHP script, along with a small amount of CSS.

I’m trying to be able to add a name at the top of the page, then click on the photos going down the page to add the name at the top to the text boxes to the right of each clicked photo.

The “name to add” text box at the top does not yet have all the names I want, but you can see how it works by typing “B”, after which several choices will be presented to you. Eventually I want to generate the array of names that are suggested here from my MySQL database, but for now they’re hard-coded.

Some of the entries are broken because one of my family member’s names has an apostrophe in it, which is messing up the PHP. I still need to add slashes, or something, but I’ll get to that later.

Thanks again.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Great! This is going to be pretty easy, because the fields have IDs
and everything is predictably laid out from a code standpoint. First
off, I recommend that you update your server side suggestion script to
use a LIKE match rather than an = (case-sensitive) match – I typed a
b (lower-case) and nothing happened.

On to the script. First, add the Prototype.js library to your page.
The easiest way to do that is with the Google CDN. Paste this in your
page head somewhere early:

<script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6/prototype.js 

"
type=“text/javascript” charset=“utf-8”>

Your keyword field is called ‘txt1’, so to access its value within a
script, you just use

$F('txt1');

Now, to “wire” your images to do the magic, you will need to attach an
observer function. Add the following script to your page head, after
you have included Prototype:

<script type="text/javascript">
document.observe('dom:loaded',function(){
	$$('img').invoke('observe','click',function(evt){
		var elm = Event.element(evt);
		var field = elm.next('input[type="text"]);
		if(field){
			var v = $F(field);
			var n = $F('txt1');
			if(!f.include(n)) field.value += ' ' + n;
		}
	});
});
</script>

Let’s step through this a line at a time, so you can get a feel for
what’s going on inside all this terseness:

  1. This is an unobtrusive wrapper. This line makes the script wait
    until the DOM (the object model of the page) has loaded, but doesn’t
    wait until everything displays on the screen. This means that the
    script is wired and ready to go even before all the pictures have
    loaded.
  2. This is a fun bit of metaprogramming. The $$ (double-dollar)
    function returns an array of elements that match the CSS selector. In
    this case, we’re simply extending all image elements on the page. In a
    page with other decorative elements, you could give your trigger
    images a special classname, and thus only select any img.thumbnail or
    something like that. The result of the double-dollar is passed to the
    invoke method, which requires an array of objects and applies the same
    method to all of them. In this case, it’s applying the ‘observe’
    method, and handing that method two variables: ‘click’, and the
    function to apply when it “hears” a click.
  3. We’re inside the function being applied to each click on an image.
    This line gets a reference to the image that was clicked on.
  4. This line gets a reference to the text field directly following the
    image that was clicked (or false, if it fails).
  5. Check to see if we have a field following the image.
  6. We do, so we get the current value of that field as a string.
  7. Get the current value of the Name field as a string.
  8. If that name is not already in the field, add a space and the name.
    (If you want to use commas to separate the names, then you would add
    that into this part of the script.)

The rest of the script is just ending the closures and loops.

Because we’ve used an unobtrusive listener, all of this scripting will
be available from the moment the page loads. Nothing on the page will
need to be explicitly changed or modified for this to just work.

By the way, if you run into problems with your autosuggest script
after including Prototype, let me know. There’s an autocompleter
available in Scriptaculous, which is built on Prototype and thus does
not conflict with it. It’s a one or two-line change from the current
script you’re using.

Walter

On Mar 11, 2009, at 11:28 AM, Joe Crnich wrote:

Here’s the URL of the page that does the keywording:

http://crnich.dyndns.org/familyphotos/php/imageupdater2.php

I’m not doing this in Freeway, but rather hand-coding it, as the
page layout is very simple and is entirely generated by the PHP
script, along with a small amount of CSS.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Typo. This should read:

		if(!v.include(n)) field.value += ' ' + n;

Walter

On Mar 11, 2009, at 12:48 PM, Walter Lee Davis wrote:

  	if(!f.include(n)) field.value += ' ' + n;

dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walt,

This looks great. I’ll try to “wire it up” when I get home from work this evening.

Mahalo nui loa! (to you haoles, that’s “Many thanks!”)

I’ll probably be back to find out how to change to the LIKE match in javascript. In PHP, I’m okay. In javascript, not so much.

Joe


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

if(string1.toLowerCase() == string2.toLowerCase()) …

Once you start feeding this name lookup from the server, then you
won’t need to do that. Your script will be asking the server for a
list of matches, and you can make that query use MySQL LIKE syntax.

Walter

On Mar 11, 2009, at 1:40 PM, Joe Crnich wrote:

I’ll probably be back to find out how to change to the LIKE match in
javascript. In PHP, I’m okay. In javascript, not so much.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walt,

Okay, I couldn’t stand the suspense, so I added your code, but it doesn’t seem to be working. Maybe we have a conflict with the suggestion javascript I’m using?

Do you have a link to the alternative text box suggestion code?

Also, in the line “var field = elm.next('input[type=‘text’]);” it looks like you’re missing a single quote somewhere. I tried both adding one after the bracket and removing the one before “input”, and neither option helped anything.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

I also commented out all the other scripts so only yours loaded, and even then I couldn’t get it to work. (I even tried adding and removing the single quote from the ‘input’ line, as noted above, in this scenario, but no dice.)


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Those single-quotes are supposed to be double-quotes inside:

ar field = elm.next('input[type="text"]');

sorry, I was typing this off the top of my head.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

And that is meant to be var field, not ar field…

Just tested this, it works fine.

The autocompleter part can be found in the script.aculo.us library, to get that to work you simply add the link to scriptaculous after Prototype:

<script src="http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.1/scriptaculous.js" type="text/javascript" charset="utf-8"></script>

Documentation is here: http://wiki.github.com/madrobby/scriptaculous and they have both an Ajax.Autocompleter (for server-side lookup) and an Autocompleter.Local (similar to what you’re using now).

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

http://scripty.walterdavisstudio.com/keywords

That’s everything from this page, wrapped up in a simple page.

Walter


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walt,

I finally got it going, after much fiddling around. It turns out that I had your javascript observer script loading in through the PHP code section, with ‘Print’ statements, and every time it ran the PHP code the “$F” was getting thrown out. That’s why your observer wasn’t working.

As soon as I moved your script up before the PHP code section (as straight HTML), it ran great. I noticed that you made the dropdown suggestions render above the photos below, and that you’ve been busy adding artistic styles to the dropdown.

My (semi-)final page is at:
http://crnich.dyndns.org/familyphotos/php/imageupdater3.php

I still need to finish the updating script. More later.

Thanks again for your help.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Looks really good, glad I could help.

Walter

On Mar 12, 2009, at 2:22 AM, Joe Crnich wrote:

I still need to finish the updating script. More later.

Thanks again for your help.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Walt,

I finally got it all hooked up the way I want.

I was going to ask you for help with another javascript to enable a button to jump from any photo or index page to the corresponding keyword page (as the index pages are already set up as a form to submit search queries), but I happened upon a solution that used an Applescript to create the keyword URL from the index or photo page URL, so I didn’t need it.

This pretty much negated the need I would have had for a password (as I didn’t want just anyone to add keywords), because without the Applescript outsiders don’t know the proper syntax to get to the keyword page(s).

If you know a quick way to add a javascript-powered button to “Keyword All” with the keyword currently in the text box, though, that might come in handy. :wink:

Thanks again for the help. I just added keywords to over 500 photos in (for me) record time because of this utility.


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

If you give all of your keyword text inputs a classname, that would
make this very simple. Let’s say you give them each the class ‘key’,
and add a new link with the ID ‘tagall’, then you would do this inside
the main unobtrusive listener function:

$(‘tagall’).observe(‘click’, function(evt){
Event.stop(evt);
var newTag = $F(‘txt1’);
$$(‘input.key’).each(function(elm){
if(!$F(elm).include(newTag)) elm.value += ’ ’ + newTag;
});
});

I’ve updated my example to include this code.

Walter

On Mar 14, 2009, at 6:39 PM, Joe Crnich wrote:

If you know a quick way to add a javascript-powered button to
“Keyword All” with the keyword currently in the text box, though,
that might come in handy. :wink:


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options

Outstanding! I got your new code added, and it works a treat!


dynamo mailing list
email@hidden
Update your subscriptions at:
http://freewaytalk.net/person/options