Easy Ajax : Part 2

In the first post of this series, we looked at a really basic AJAX example. We simply pulled the time and date into our page via an AJAX call.  In this post, we'll take things a step further by passing a variables in and using it in the output.

For this example, we'll take an input string and output it in reverse.

Here is a working sample:



This example is also pretty simple. We'll start again by including the Prototype script in the head of our document:

<script type="text/javascript" src="prototype.js"></script>

Next, create the necessary form fields and buttons with this code:

<input type="text" id="input_string" />
<input type="button" value="Reverse It" onclick="run_ajax();" />
<div id="ajax_div"></div>

And now for our Prototype magic...

<script type="text/javascript">        
    function run_ajax() {
         var url = '/ajax/easy2.cfm';
         var pars = 's='+escape($F('input_string'));
         var target = 'ajax_div';
         var myAjax = new Ajax.Updater(target, url, {method:'get', parameters:pars});
    }
</script>

A few things to note in the above code block.. The line starting with "var pars" passes our data over to the AJAX page. We use $F() to get the value of the field with id "input_string" $F() is shorthand syntax that is built into Prototype and can save you lots of time and headaches. The old way was to write out document.getElementById() each time, so this is a lot shorter. We also escape the string in case there were any weird characters.

On the last line, in the Ajax.Updater call, we added braces to enclose any options including a reference to our parameters. In this case, we're specifying that this will be a get request, so we'll be expecting "URL." parameters in ColdFusion. A POST request would yield "FORM." parameters in our ColdFusion page.

Lastly, we need the ColdFusion page which will receive our call via AJAX and pass back the results. That page is actually really simple in this example, containing the following code:

<cfsetting showdebugoutput="no">
<cfoutput>
#reverse(url.s)#
</cfoutput>

That's it! We've now got the basics covered and you should be able to start working some magic!

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Adam's Gravatar What Teddy may be asking (or not, so I will) is how do you invoke a method in a cfc using prototype. I assume you would pass your function parameters using the param, but where does the actual function name go? In the url like myCFC.cfc?myFunction or is it something totally different?
# Posted By Adam | 8/2/07 9:47 AM
Anonymouse's Gravatar Good. This was really helpful thank yuo
# Posted By Anonymouse | 12/9/07 1:38 AM

Archives By Subject

Calendar

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31  

Latest from MXNA

Recent Entries

No recent entries.

Recent Comments

BulletProject Tracker v2.0 Released
Scott said: Joe- Great app - I use it at work. I do have a question, though. I wanted to install this on my host... [More]

BulletThe Importance of HTMLEditFormat()
Andrew said: Just do it when outputting to the screen, not when inserting/updating data in the database otherwis... [More]

BulletProject Tracker v2.0 Released
Aaron Bodell said: I came across this app while searching the RIAForge for project trackers. I'm really impressed with ... [More]

BulletProject Tracker v2.0 Released
Richard said: Thanks Joe - Looking forward to testing the new version! Is there somewhere we can make suggestions... [More]

BulletAmazon S3 REST Wrapper
Zac Spitzer said: I've been hacking away on the CFC and I have fixed numerous bugs and added support for compression, ... [More]

RSS


Search

Sponsored Links

Wimpy MP3 Player
Wimpy MP3 Player
easy streaming media
for your coldfusion site



Subscribe

Enter your email address to subscribe to this blog.

Tags

ajax coldfusion