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

Feed temporarily down.

Recent Entries

No recent entries.

Recent Comments

BulletProject Tracker v2.0 Released
Mike said: great app! thanks heaps. found a few bugs in the initial install... -forgot.cfm - no need to inclu... [More]

BulletProject Tracker v2.0 Released
Nathan said: Hi Joe I went to check out the demo site http://ajaxcf.com/project... and it throws an error. Inval... [More]

BulletProject Tracker v2.0 Released
Lachie said: Wow thanks for creating a version 2. I have extensively used version 1 for my projects. It works gre... [More]

BulletProject Tracker v2.0 Released
Steve W said: Joe, You style sheet is missing because a period was left out of the path. <link rel="styl... [More]

BulletProject Tracker v2.0 Released
Mark Mandel said: Joe, Great work! I can't wait to have a play! Btw, your CSS isn't rendering on your post view pag... [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