1 of 2
1
How to: Have a basic AJAX submission with stars
Posted: 09 February 2008 09:30 AM   [ Ignore ]
Member
Avatar
RankRankRank
Total Posts:  51
Joined  2007-07-16

Ok, I’m sure lots of people will find this useful so here are my black and white instructions on how to get nice looking stars with click-to-submit AJAX submission.

Things you’ll need:

1. A copy of prototype.js, probably best to get the latest build, though I am using 1.5.1.1
2. This zip file of assets. This will give you the 3 star images you need to test with, plus the .psd (CS3) should you want to make changes. There’s also the loading animation in there, though you can go make your own easily enough.
3. ExpressionEngine and an installed copy of the Ratings module!

Ok, first things first: upload the assets to your /images/ folder.
For this example, I’m going to upload them to /images/assets/

Next, install prototype.js
This is merely a case of copying in the javascript to a blank template of the type ’static‘.
For this example I’m going to create it in /_incs/prototype.js

Now it’s time to create the template to return the messages that are fed back when a user submits their rating. [/size]
For this example I’m going to call it /_incs/ajax_rating_message
This is another easy step, simply put this in to the template:

{message}

OK, now we need to create our main page, the weblog entry page.
You probably already have this setup, add just need to add in the new code, so I’m going to keep the template as clean as possible.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head>
<
meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<
title>My rated entry</title>

<!--
This is the link to your prototype.js file -->
<
script type="text/javascript" src="/_incs/prototype.js" ></script>

</head>
<
body>

{exp:weblog:entries weblog="my_weblog" disable="trackbacks" limit="1"}

{title}

{summary}

{body}

<!-- NOTE: The template parameter pointing to our message template doesnt seem to like having a leading / in its path  -->
{exp:rating:form entry_id="{entry_id}" form_name="rating" form_id="ratingform{entry_id}" template="_incs/ajax_rating_message"}

<script type="text/javascript">
<!--

function
starsOver(target) {
for (i=1; i<=5; i++) {
  
if (i <= $(target).alt) {
   
$('star'+i).src = "/images/assets/star-over.png";
  
} else {
   
if (i <= $('rating').value) {
$('star'+i).src = "/images/assets/star-on.png";
   
} else {
$('star'+i).src = "/images/assets/star-off.png";
   
}
  }
}
}
function starsOut(target) {
for (i=1; i<=5; i++) {
  
if (i <= $('rating').value) {
   
$('star'+i).src = "/images/assets/star-on.png";
  
} else {
   
$('star'+i).src = "/images/assets/star-off.png";
  
}
}
}
function setStar(target) {
for (i=1; i<=5; i++) {
  
if (i <= $(target).alt) {
   
$('star'+i).src = "/images/assets/star-on.png";
  
} else {
   
$('star'+i).src = "/images/assets/star-off.png";
  
}
}
$('rating').value = $(target).alt;
new
Ajax.Updater('ratingresult', '/', {
  onComplete
:function(request, json){
   Element
.hide('ratingloading')
  
},
  
onLoading:function(request, json){
   Element
.show('ratingloading')
  
},
  
parameters:Form.serialize('rating{entry_id}'),method:'post',asynchronous:true
}
); return false;
}
-->

</script>

<img src="/images/assets/spacer.gif" width="21" height="21" id="star0" alt="0" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)"/>
<
img src="/images/assets/star-off.png" width="21" height="21" id="star1" alt="1" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off.png" width="21" height="21" id="star2" alt="2" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off.png" width="21" height="21" id="star3" alt="3" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off.png" width="21" height="21" id="star4" alt="4" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off.png" width="21" height="21" id="star5" alt="5" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
input type="hidden" name="rating" id="rating" value="0"/>

<!--
This is our loading image -->
<
div id="ratingloading" style="display: none">
<
img src="/images/assets/loading.gif" width="16" height="16" alt="Loading"/>
</
div>

<!--
This is the div which will contain the text fed back from our message template -->
<
div id="ratingresult">
</
div>

{/exp:rating:form}

<p>{exp:rating:count entry_id="{entry_id}"}Number of ratings: {rating_count}{/exp:rating:count}</p>

{/exp:weblog:entries }

</body>
</
html>

And finally - try it out!

I am hoping this will help a few people out.

For those of you with less experience - If you can’t get it to work, try figuring it out. Try Google, hunt for an answer and if there’s still no luck, then return here wink

Profile
 
 
Posted: 13 February 2008 09:40 AM   [ Ignore ]   [ # 1 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  2320
Joined  2006-10-18

Tony, as always, thanks for your contributions!! smile
This will definately come in handy wink

 Signature 
Profile
 
 
Posted: 03 March 2008 01:48 PM   [ Ignore ]   [ # 2 ]
Jr. Member
RankRank
Total Posts:  31
Joined  2007-03-01

4 hours and way too much debugging helped me realize that in your example, you have

parameters:Form.serialize('ratingform{entry_id}'),

INSTEAD OF

parameters
:Form.serialize('rating{entry_id}'),

Your form.serialize is trying to serialize a form that doesn’t exist.

Profile
 
 
Posted: 03 March 2008 02:08 PM   [ Ignore ]   [ # 3 ]
Member
Avatar
RankRankRank
Total Posts:  51
Joined  2007-07-16

Gah!

Sorry, I’ll correct that… I can only put it down to human error wink

Profile
 
 
Posted: 03 March 2008 02:11 PM   [ Ignore ]   [ # 4 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  2320
Joined  2006-10-18

...and I thought you weren’t human LOL

 Signature 
Profile
 
 
Posted: 09 April 2008 07:13 AM   [ Ignore ]   [ # 5 ]
Newbie
Rank
Total Posts:  23
Joined  2007-01-16

I have no success with this tutorial.

I used the same code as in the example. But on the page I see only the not working stars (please have a look at the screenshot).

In the source code of the page, the entry code is visible, put not the entry text on the real page.

And I have the following error message:

Line: 95
Char: 1
Error: Object expected
Code: 0

Thanks for your help.
MISC

Image Attachments
screenshot.jpg
Profile
 
 
Posted: 09 April 2008 07:21 AM   [ Ignore ]   [ # 6 ]
Member
Avatar
RankRankRank
Total Posts:  51
Joined  2007-07-16

Got a link to a page. ‘misc’ ? rasberry

Profile
 
 
Posted: 09 April 2008 08:32 AM   [ Ignore ]   [ # 7 ]
Member
Avatar
RankRankRank
Total Posts:  51
Joined  2007-07-16

Ok I’m not having a good day with the messaging so I’m posting back here.

I had the HTML wrong in this, for a very strange reason.

Make sure you HTML include is something like this:

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

Profile
 
 
Posted: 10 April 2008 12:32 AM   [ Ignore ]   [ # 8 ]
Newbie
Rank
Total Posts:  23
Joined  2007-01-16

Now, it works perfect.

Many thanks,
MISC

Profile
 
 
Posted: 29 April 2008 05:06 AM   [ Ignore ]   [ # 9 ]
Jr. Member
RankRank
Total Posts:  46
Joined  2008-04-29

New here.. A potential customer.. I’m not exactly an aficionado when it comes to AJAX and such, but I was hoping this module would come to use.. If my understanding is correct, this allows for ratings to be submitted and for the page to not reload? Is this similar in function to http://www.nicestylesheet.com’s implementation I saw in an earlier thread?

I’m interested in the module, and possibly quite a few other ones, but didn’t want to get ahead of myself and wanted to know what to expect.. So the default behavior of the Ratings module is via a select box?

Profile
 
 
Posted: 29 April 2008 05:30 AM   [ Ignore ]   [ # 10 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  702
Joined  2004-03-30

Mr. Dim Sum,

Rating submission via AJAX is something that people add-on to the native Rating module functionality. That does not come ready out of the box. So you’ll br using a select box or radio buttons along with a form submission in the beginning.

mk

 Signature 

Mitchell Kimbrough

Profile
 
 
Posted: 29 April 2008 05:57 AM   [ Ignore ]   [ # 11 ]
Jr. Member
RankRank
Total Posts:  46
Joined  2008-04-29

So the solution provided by the opening poster is still relevant to this solution?

Profile
 
 
Posted: 29 April 2008 07:21 AM   [ Ignore ]   [ # 12 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  2320
Joined  2006-10-18

Yep! LOL

 Signature 
Profile
 
 
Posted: 18 May 2008 11:01 AM   [ Ignore ]   [ # 13 ]
Newbie
Rank
Total Posts:  5
Joined  2007-03-24

Thanks to Pie Man, first ofall, for helping me understand how to get the basic module working!  Now I’m on this stage and running into a couple of problems.

(other problems erased, I figured it out)

New problem; How do I get it to display the average rating (in stars, of course) as the default?  Like, before a registered user logs in and votes, they can see 3 stars colored in.  As it stands now, all we see are the 5 grey stars, and then just our own vote after that.

Profile
 
 
Posted: 20 May 2008 07:52 AM   [ Ignore ]   [ # 14 ]
Administrator
Avatar
RankRankRankRank
Total Posts:  2320
Joined  2006-10-18

JimK,

That’s a great idea… I’m not quite sure how you would get this to work with the above code, as the Ajax submission is dependant on the exact ID’s of the stars, etc…

I supposed the simplest way to do this would be adding this to the code:

{exp:rating:stats entry_id="{entry_id}"}
{if overall_count
>= "1"}_on{/if}, etc
{
/exp:rating:stats}

You would stick that onto the image so the code could determine whether to show a coloured or greyed out star: smile

<img src="/images/assets/spacer.gif" width="21" height="21" id="star0" alt="0" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)"/>
{exp:rating:stats entry_id="{entry_id}"}
<img src="/images/assets/star-off{if overall_count >= "1"}_on{/if}.png" width="21" height="21" id="star1" alt="1" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off{if overall_count >= "2"}_on{/if}.png" width="21" height="21" id="star2" alt="2" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off{if overall_count >= "3"}_on{/if}.png" width="21" height="21" id="star3" alt="3" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off{if overall_count >= "4"}_on{/if}.png" width="21" height="21" id="star4" alt="4" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
<
img src="/images/assets/star-off{if overall_count >= "5"}_on{/if}.png" width="21" height="21" id="star5" alt="5" onMouseOver="starsOver(this.id)" onMouseOut="starsOut(this.id)" onClick="setStar(this.id)"/>
{/exp:rating:stats}
<input type="hidden" name="rating" id="rating" value="0"/>

Of course, this would require you to have your grey star be named: star-off_on.png
And your coloured star to be named: star-off_on.png
That of course sounds clunky, but of course, you have control over the naming wink

 Signature 
Profile
 
 
Posted: 03 June 2008 08:53 AM   [ Ignore ]   [ # 15 ]
Newbie
Rank
Total Posts:  3
Joined  2008-06-02

Hey guys,

Thanks for posting this, but I am having some trouble getting this to work.  I’m able to get the stars to display, and click the star to make it active, but nothing ever happens.  I don’t believe it’s actually posting the rating at all, and looking at the Ratings CP in the module confirms that.  When I look in firebug I’m showing the following error (referencing the prototype.js file) when I click on the one of the stars to submit my rating:

$(form) has no properties
getElements
("rating6")
serialize("rating6", undefined)
setStar("star4")
onclick(click clientX=0, clientY=0)
return
$A($(form).getElementsByTagName('*')).inject([],

Have you all seen this behavior before?

Any help would be greatly appreciated!

Profile
 
 
   
1 of 2
1