Skip to main content
Participating Frequently
August 27, 2009
Answered

Need Coldfusion 8 and jquery help

  • August 27, 2009
  • 1 reply
  • 843 views

Hey guys,

I am using Coldfusion 8 and am trying to do something exactly like the functionality in Facebook where you post or "Share" something on a user's wall.

All I want to do is have an input text box do an ajax post and then do an ajax load of the latest posts so that it shows that latest posting on top of the existing ones.

I have been reading numerous sites on various json and coldfusion tuts, however I can't seem to find one that deals with posting and refreshing data after the post using ajax/json

If anyone can give me a quick and dirty example I would greatly appreciate it.

Thank you in advance!

Chris

    This topic has been closed for replies.
    Correct answer kat_dev

    Actually, you don't have to get the all posts, you just have to get the latest one, and to show as the first post, instead of append, do a prepend.

    This will be much faster and easier, you really don't need JSON for this, return format plain will do.

    with returnformat=json, you can return anytype, query or array or structure. To see the what coldfusion returns, use Firebug plugin in firefox

    add this line to the callback function

    console.log(data)

    it will show the data object in the console, you can expand it to see what it has and how to use it;

    1 reply

    Inspiring
    August 27, 2009

    Hi,

    You can do all this in one step, save and load at the sametime,

    if you don't want to do that, you call load within callback fn.

    <p id="posts">
    <!-- All posts --->
    </p>
    <form id="myform">
    <label for="post">New Post:</label><input name="post">
    <a href="">Post</a>
    </form>

    <script type="text/javascript">
    $(document).ready(function(){
    $("#myform a").click(function(e){
      post = $("#myform")[0].post.value;
      url = "mycfc.cfc?method=saveAndReturnPosts&returnFormat=json&post=" + post;
     

    /* one step update and load */
      $.getJSON(url,function(data){
        $("#posts").empty();
        for (i=0;i<data.length;i++){
        $("#posts").append('<div class="myclass">'+data+'</div>');
       }

    /* if you want to do ajax load call do it here , in that case you can do a get instead of getJSON
    eg $("#posts").load("mycfc.cfc?method=returnPosts&returnFormat=plain") */

      });
     
    return false;
    });

    });
    </script>

    mycfc.cfc
    <cfcomponent>
    <cffunction name="getCarModels" access="remote" returntype="array">
      <cfargument name="post" type="string" required="no">
      <cfset arrRet = arraynew(1)>
      <!--- write logic to save the post and return all the posts --->

      <cfreturn arrRet>
      </cffunction>
    </cfcomponent>

    tryston00Author
    Participating Frequently
    August 27, 2009

    Wow thank you so much for all your help.

    One last question :-)

    In the cfc, once I save the post and then retreive all posts, how do I ensure they are retrieved as an array since I am to return an array of posts?

    <cfset arrRet = arraynew(1)>
      <!--- write logic to save the post and return all the posts --->

    <cfreturn arrRet>

    Thank you again!!

    Chris

    kat_devCorrect answer
    Inspiring
    August 27, 2009

    Actually, you don't have to get the all posts, you just have to get the latest one, and to show as the first post, instead of append, do a prepend.

    This will be much faster and easier, you really don't need JSON for this, return format plain will do.

    with returnformat=json, you can return anytype, query or array or structure. To see the what coldfusion returns, use Firebug plugin in firefox

    add this line to the callback function

    console.log(data)

    it will show the data object in the console, you can expand it to see what it has and how to use it;