one quirky thing to watch
returned objects are always lower case
var resp = obj.myid + obj.mytime
and not
var resp = obj.myId + obj.myTime
Main Topics
Browse All TopicsHello All,
I want to do something similar to the way twitter searches use AJAX to create the "Real-Time Results".. It seems that the database is polled every 15 seconds or so and the number of new records is displayed to the user after each pull. I am working on a Coldfusion 8 server with a MS SQL database and I need to implement something like this ASAP.
I posted a question similar to this earlier and an expert was assisting me; though he was indeed extremely helpful the thread got lost in the shuffle and I need some help with this ASAP. I believe the solution will involve jquery javascript library (http://jquery.com), coldfusion components, cfajaxproxy to work with the cfcs, and the java setInterval() function.
I'm tight on time; can anyone illustrate some sample code for an implementation of the simplest version of this? Hopefully I can then understand and adapt it to my needs.
Thank you in advance!!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
sorry it took me so long to get back to this that you even had to opened a new question...
attached is a working example (i.e. prrof of concept).
it uses a query stored in session scope, so make sure sessions are enabled in your application.
put all the files in same folder, remove .txt extensions from them (EE does not allow uploading .cfm files, even inside a zip archive...).
javascript code is commented, but do ask if something is unclear.
Azadi
Thanks; I got the sample project working dandy, now I am just trying to apply it to my current project. I'm having a little trouble modifying the query.cfm because my DB is already setup (and I'm not creating it at page launch with querynew as you are in this sample). Basically I don't know how to associate session.qSearch with my prebuild DB instead of the one your creating here (or if its even necessary).
Still trying to figure it out on my own over here as well; thanks!
>> Basically I don't know how to associate session.qSearch with my prebuild DB instead of the one your creating here (or if its even necessary).
it is not necessary - you can (and should) use your own query. and you do not need to put it in session scope either - that was just for my example to work without extra db setup.
just remember to change the result variables used in the checkResultsCB js function - they use the query's column names:
in my example the query returned columns id and name, and the js function used r.DATA.id and r.DATA.name.
you will need to change that to use columns returned by your query.
also remember that column names are case-sensitive in a way - they should be referenced in checkResultsCB function in same case as they are in the query sql in the cfc function.
Azadi
Thanks again! I am very close to getting it working. Everything is okay except the part where the notice for new results displays; I narrowed it down, keeping in mind what you said about case sensitivity; but I notice that the alert in the below code does not show up (wheras a similar alert in your original code does, or if i put like alert("Yo") instead)...something goes wrong here; its almost like the CFC function fails for some reason; i removed the query and tried it separately and it worked so I don't think its just a SQL error.
Thanks!
hmm.. nothing wrong jumps out at me in that code...
you say when you change alert(r.DATA.RecordID[0]);
if so, there's probably something wrong with the data returned by cfc call and we need to do some debugging...
the best tool for this is Firefox+Firebug add-on - if you are not already using them you should start now.
for starters, add console.log(r); as the first line in the checkResultsCB function, reload the page with Firebug open, copy the output from Firebug's console tab and post it here.
Azadi
need to see what's in the DATA object.
in firebug console tab you should see lines like GET http://... appear every 10 seconds.
click on one of those to expand it, then click on Response tab and copy the response text and post here.
(if you can't see the http requests listed in the console tab - click on the little arrow on the tab title and select "Show XMLHTTPRequests" option)
you should also be able to click on the {ROWCOUNT=... lines to see the full object in the DOM tab. next to DATA object there look at the names of properties returned - is there one called RecordID ? is it in the same case as you are using or different?
Azadi
ha, you beat me to it!
>> "DATA":{"RECORDID":[47,46,
as you can see, the column names are returned in UPPERCASE.
you need to use RECORDID and MESSAGE (all uppercase) instead of RecordId and message in the js code.
i think the difference is that my data was returned from a QoQ, while yours is returned from a real db query - thus mine was being returned in the case as i had the column names in the QoQ's SQL, but yours is converted to all uppercase.
Azadi
just to be clear:
change your
alert(r.DATA.RecordId[0]);
to:
alert(r.DATA.RECORDID[0]);
and also change
if (r.DATA.RecordID[i] > curMaxID) tr += '<tr class="newresult"><td width="20">'+r.DATA.RecordI
to:
if (r.DATA.RECORDID[i] > curMaxID) tr += '<tr class="newresult"><td width="20">'+r.DATA.RECORDI
as well as curMaxID = r.DATA.RecordID[0]; to curMaxID = r.DATA.RECORDID[0];
Azadi
cf returns structures create with dot notation (i.e. somestructure.somekey) to all uppercase. same goes for returning queries which are a special case of structures.
structures created using array notation (somestructure['somekey'])
this is not an issue when your returned data is consumed by cf code, which does not care about the case, but it makes all the difference when returned data is consumed by javascript which is case-sensitive.
i didn;t know that QoQ were treated differently by cf (or maybe it's not QoQs but queries created using querynew() function... will need to test it out) and that threw me off a bit, even though i knew that cf returns most things in uppercase.
Azadi
>> why did it not matter for message and it did for recordID?
my guess would be because message was in all lower-case, while RecordID was in mixed-case... but i will need to run some tests to say for sure, and to see how exatly the case matters (i.e. what would have happened if you had it named as recordID...)
Azadi
Hmm okay; well at least it was interesting!
I have some follow up questions on this project here; simple ones (I think) that I may somewhat know the answer to already. Should I post them here; or should I close this out and start a new question. You've been very helpful and I want to make sure you get the proper points ;)
keep posting them here - it will be easier to follow the code and conversation in one place than having to jump from one question to another. it will probably also be better for someone looking for this solution later...
i also think you can close the first question you had opened on this subject (the one i had neglected for too long) and request full points refund for that one. explain that you opened a new question on exactly same subject and link to this question in the reason to close the first one.
so, what's the next question? :)
Azadi
K sounds good, thanks! ;)
Okay; as we are now well aware the records currently consist of RecordID and Message; this was a simplification of what I am working on. There will actually be another field "category" that could be one of a few things (lets say 3). So for instance, this table might have records that look like
1, myMessage, category1
2, myMessage, category2
3, myMessage, category3
4, myMessage, category2
and so on. So basically the page is going to have a a few separate sections (corresponding to the categories), and I want this kind of update noticing of new records happening in each section, but only for message with the category of that section. I hope that makes sense. Now; I think I understand what I have to do; basically make 3 separate database searches instead of one (one for each category) and handle it all similarly. I just want to make sure this is the most efficient way; as if say there are 20 questions; would it be a problem to make these 20 search calls every 10 seconds? Or is there some kind of magic SQL way to make one DB call and then parse it into 20 different result sets (that then are what is used to do the update noticing) etc.
That was a little convoluted but I hope it made sense!
my first thought would be that, yes, you need to make 20 separate calls to the cfc - one for each category...
but then... all you really need is a separate new results counter for each section, and you can return all data in one query in one call. of course, you won't be able to use the very handy ROWCOUNT variable - you will have to create a custom counter for each category in js.
let me adjust my sample code to include multiple categories - you can then have a look at the code to see how to do it.
Azadi
Okay; thanks. I'm thinking that I might get stuck having to make seperate DB calls for each category in case I want to do more specific things. Like for instance lets say on page start up I'd want to start out displaying the 10 most recent message for each category; I might have to do that doing each separate call and ordering them be RecordID (or timestame) DESC and limiting to 10...but then again maybe that can be done with SQL Parsing magic too.
Actually; maybe not just on page start up for checking the categories; but at every interval (in case for some reason a category gets added or dropped while the page is sitting there. Sorry for my rapid fire posts; just want to get any information up here before you dive in; don't want to lead you in circles.
well, here it is!
categories support and all - with just one query and one ajax call.
the code (js mostly) is significantly more complex than in the previous version, all because of multiple categories of results.
the code is commented as much as i could.
2 important issues:
1) your data must be returned by 1 query. it is almost always possible to do this, but to say for sure i will need to see your db structure.
2) IDs of html elements in the code are very important - especially the ones that include the Category identifier (#CAT#, which in my example it is a numeric value, but it could be a sting es well, though some js may have to edited to account for this). js code uses these IDs extensively and won;t work without them.
have a look/play with the code, and shout if you have any questions.
Azadi
I got everything working with my DB data perfectly; just wrestling with formatting changes ugh ;) Now my boss wants it so there is just one table, with a row in it for each category, and only 1 record (the newest one) showing at a time with a link to display older records which will make some older records appear below that in "subrows"
Oiy!
I'm trying to figure out how to adapt based on your extremely well commented code; but your java kung fu is far superior to mine so its been slow going. I'm currently focusing on this:
var tr = '<tr class="newresult"><td width="20">'+r.DATA.ID[i]+'
as that is clearly related.
@andrewaiello:
your new requirement is rather different from your original question...
i will be happy to help you with it if it is still relevant and if you open a new question for it.
@angelIII:
i believe i have provided a solution to the original question, which has been acknowledged by the OP in his last comment. in case the OP does not finalize the question in time and you need to close it, i will appreciate if you kept that in mind.
Azadi
@angelll
Sorry I forgot about this for a while; I have closed the quesiton with aza's answer.
@aza
I apologize for being away for a while! Yes, I am still struggling with this other aspect; how best do you think I should phrase this as a new quesiton (since it obviously relies a lot on the info here). Thanks!
Business Accounts
Answer for Membership
by: SidFishesPosted on 2009-11-03 at 11:50:16ID: 25732775
i don't do jquery or use many of the builtin in cfajax functions but here's a basic concept for you using jsmx ajax engine from lalabird.com, a bit of js and a simple cfc you can fancy it up however you like and replace the function with a query. If you need to pass arguments to the cfc just use param
mehiddenin putfield') .value + 'someothercriteria' + documentGetElementById('so meotherhid deninputfi eld').valu e; e.com/util .cfc? metho d=getUpdat e',getUpda te_respons e,param);
e.com/util .cfc? metho d=getUpdat e',getUpda te_respons e,param);
pdatePanel ').innerHT ML
pdatePanel ').innerHT ML = existing + '<br>' + resp; ;
ie:
param = 'someid=' + documentGetElementById('so
http('POST','http://websit
dbrefresh.cfm
<script type="text/javascript" src="engine.js"></script>
<script type="text/javascript">
function init(){
param = '';
http('POST','http://websit
}
function getUpdate_response(obj){
var existing = document.getElementById('u
var resp = obj.myid + obj.mytime
document.getElementById('u
setTimeout("init()",15000)
}
</script>
<body onLoad="init()">
<div id="updatePanel">
Initial Text
</div>
</body>
util.cfc
<cfcomponent>
<cffunction name="getUPdate" returntype="struct" access="remote">
<cfset update = {myid = #createuuid()#,
mytime = #now()#}>
<cfreturn update>
</cffunction>
</cfcomponent>