+ Reply to Thread
Results 1 to 4 of 4

Thread: Ajax with Mootools: making a product rating(JS only)

  1. #1
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Ajax with Mootools: making a product rating(JS only)

    Ajax with Mootools
    hi codecallers,
    this tutorial will be about using the Mootools JS library with ajax, if you dont know a thing about ajax you will do just fine with mootools. but you need to have know some javascript to keep up.


    when to use Mootools library with ajax?

    -when you already have the mootools core library loaded in the page for other uses
    -when you are tired of cross-browser errors and dont care about the size
    -when you use it for more than once in a single page, for example you dont load 120kb of JS file just to move a picture around. make the best out of mootools when its already loaded with the page
    -check this blog out

    im using core builder to generate a mootools JS file, what classes will i need?
    you will need these 2 classes for ajax:
    1.Request
    2.Request.HTML
    theres "Request.JSON" but you will only need it if you want to use JSON instead of ajax.
    when you select these 2 the other needed classes will be checked automatically
    after you download the mootools-1.2.X-core.js, link it to the page you will use mootools in it like this:
    Code:
    <script type="text/javascript" src="mootools-1.2.3-core.js" /></script>
    now you are ready to write some JS.

    Request. how to?
    theres nothing easier than using mootools to send request.
    lets assume that we are using AJAX to let users rate a certain product without refreshing the whole page, for this purpose we will need a PHP (or any server-side language) page to accept the rating of a user and store it in database, and ofcourse a page that will send the AJAX request.
    in this code we will write JS script to initialize a Request object and send the rating to the server-side page
    Code:
    var my_request=new Request({
    url:'/rate.php',  //page URL
    method:'get'   //Method (get or post)
    });
    my_request.send('rate=5&product_id=69');//data sent
    //or you can give the send() function the parameters 
    //as an object for better readability
    my_request.send({
    rate:5,
    product_id:69
    });
    simple eh? you can spare some chars and do this instead:
    Code:
    new Request({
        url:'/rate.php', method:'get'
    }).send({
               rate:5,
               product_id:69
        });
    you can also specify the GET/POST parameters while initializing the Request object like this:
    Code:
    new Request({
        url:'/rate.php', method:'get'
        data:{
               rate:5,
               product_id:69
        }
    }).send();
    the code above will succeed in doing what its supposed to do, but that isnt enough for us we want to notify the user that the rating was done successfully or failed because of an error or because he already rated that product.
    and thats why i like Mootools when you use regular you write at least 2 functions; sending a request, accepting the response. in mootools you can do all that while initializing the Request object by using these functions:
    onSuccess : when the request is sent and received a successful response from the server. it has 2 parameters; responseText and responseXML
    onFailure : when the server sends an unsuccessful response
    onCancel : when you cancel the Request by sending a new one from the same Request object. or using the "cancel()" function
    onException : happens when theres an exception during setting the headers of the request object (rare), in that case you will need to check your data parameter(or object)
    onRequest : fires up when you use the "send()" function
    onComplete : like onFailure and onSuccess.
    now lets use them in our code:
    Code:
    var my_request=new Request({
    url:'/rate.php',  
    method:'get',
    onSuccess   : function(serverResponse){
             //if this part of code was reached then everything is going fine :D
             if(serverResponse==1)alert('thank you for rating this product');
                  else   if(serverResponse==0) alert('sorry you cant rate the same product twice');
    },
    onFailure : function(){
    alert('sorry we have an internal error, try again later');
    }
    });
    my_request.send({
    rate:5,
    product_id:69
    });
    pretty awesome and easy, right?
    as you may have noticed the code above, the server side sends either a "1" or "0"; one for rating successful and 0 for an error and produces the appropriate "alert()" message, but what if we want to use the response text to update a DIV's(or other element) innerHTML, which is a very common thing to do with ajax. thats why we have the "Request.HTML" object

    using the Request.HTML:
    from its title you can tell that this object deals with HTML elements, it can be used to load the content of a certain HTML DIV from the server-side. for example fetching a part of the page that wasn't loaded initially.

    how to use it
    its same as the regular Request n its usage but theres 2 extra things:
    1. update:
    check this code:
    Code:
    new Request.HTML({
    url:'/rate.php', method:'get',
    update: $('confirmMessageDiv'),
    onFailure : function(){
    alert('sorry we have an internal error, try again later');
    }
    }).send({rate:5,product_id:69});
    really simple, in the "update: $('confirmMessageDiv')" we used the DIV id that we will update it's content, in this one we assume that the server-side page processes the rate and sends back a confirmation message as text(or HTML)
    be careful this doesn't support JS like any regular AJAX request
    2."Element.load()":
    you will love this:
    Code:
    $('confirmMessageDiv').load('/rate.php',data:{rate:5,product_id:69});
    this replaces all the previous code in just a line

    theres one last trick:
    Code:
    $('someFormId').send('url.php');
    this one takes a form ID and sends it in ajax.

    thats all, if you liked it +rep me
    check Amrosama.Codecall.net - Power of the moo-stash! Mootools for mootools classes
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Ajax with Mootools: making a product rating(JS only)

    That is very cool and useful! Many websites are now using these interactive rating stars. Can we see an example on your website?

    +rep

  4. #3
    Join Date
    Aug 2007
    Location
    Gizeh, Al Jizah, Egypt, Egypt
    Posts
    8,675
    Blog Entries
    12
    Rep Power
    81

    Re: Ajax with Mootools: making a product rating(JS only)

    you mean a full rating script with php?
    sure ill write it here on CC and post it in PHP forum soon
    yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
    Code:
    eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
    www.amrosama.com | the unholy methods of javascript

  5. #4
    Join Date
    Aug 2009
    Location
    ~/
    Posts
    918
    Rep Power
    19

    Re: Ajax with Mootools: making a product rating(JS only)

    Awesome Tutorial + rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Photo rating
    By r2dstudio in forum PHP Development
    Replies: 1
    Last Post: 01-12-2011, 05:05 AM
  2. Help on PHP rating System
    By liri ayekpam in forum PHP Development
    Replies: 8
    Last Post: 10-06-2009, 01:53 PM
  3. Give a Rating, Get a Rating
    By ZekeDragon in forum The Lounge
    Replies: 9
    Last Post: 08-24-2009, 09:32 PM
  4. Alexa Rating
    By dirkfirst in forum Marketing
    Replies: 25
    Last Post: 07-05-2008, 07:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts