Jump to content
Larry Ullman's Book Forums

Updating User Rating - Unexpected Token (Function Call Issue?)

Recommended Posts

So I have been writing my own code (ie not from the book) and am trying to use Ajax to do a star rating. My problems it that I keep on getting the following error:



Uncaught SyntaxError: Unexpected token (


I have pointed out which line below is giving me the error but I am having problems finding out where the core of my problem is.. I have had problems before with defining functions and calling them. Why am I getting this error? I just want to call the newStarRating function at this point with the given parameters. Any help would be appreciated! For the record I realize there may be multiple problems with my code as I am a beginner.



U.addEvent(U.$('rating_star_1'), 'mouseover', function(){ newStarRating(1, "yellow_full"); } );
U.addEvent(U.$('rating_star_2'), 'mouseover', function(){ newStarRating(2, "yellow_full"); } );
U.addEvent(U.$('rating_star_3'), 'mouseover', function(){ newStarRating(3, "yellow_full"); } );
U.addEvent(U.$('rating_star_4'), 'mouseover', function(){ newStarRating(4, "yellow_full"); } );
U.addEvent(U.$('rating_star_5'), 'mouseover', function(){ newStarRating(5, "yellow_full"); } );
U.addEvent(U.$('rating_star_6'), 'mouseover', function(){ newStarRating(6, "yellow_full"); } );
U.addEvent(U.$('rating_star_7'), 'mouseover', function(){ newStarRating(7, "yellow_full"); } );
U.addEvent(U.$('rating_star_8'), 'mouseover', function(){ newStarRating(8, "yellow_full"); } );
U.addEvent(U.$('rating_star_9'), 'mouseover', function(){ newStarRating(9, "yellow_full"); } );
U.addEvent(U.$('rating_star_10'), 'mouseover', function(){ newStarRating(10, "yellow_full"); } );

if (mysqli_num_rows($already_reviewed_query_result) == 1) {
	 echo "U.addEvent(U.$('star_rating_div'), 'mouseout', function(){ newStarRating(" . $current_user_review_rows['review_score'] .", 'blue'); } );";
 else {
	 echo "U.addEvent(U.$('star_rating_div'), 'mouseout', function(){ newStarRating(0, 'yellow_full'); } );";
	 echo "/* \$already_reviewed_query is:*" . $already_reviewed_query . "*/";

function newStarRating(starNumber, color) {
 'use strict';
 var i = null;
 for (var i = 1; i <= 10; i++) {
	 document.getElementById('rating_star_' + i).src='../images/white_star_17px.png';
 } //end of star color reset

 var j = null;
 for (var j = 1; j <= starNumber; j++) {

	 document.getElementById('rating_star_' + j).src='../images/' + color + '_star_17px.png';



function starSubmitFxn(starNumber) {
'use strict';

//Get a reference (get element by ID) to the entered username value:
var ajax = getXMLHttpRequestObject();

var currentUsername = '<?php echo $current_username ?>';
var currentEntityId = '<?php echo $entity_id ?>';

		 ajax.onreadystatechange = function() {
			 if (ajax.readyState == 4){
				 //Check the status code:
				 if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) {
					 for ( var r = 1; r <= 10; r++){
						 if (ajax.responseText == r) {
							 newStarRating(r, "blue");//end of newStarRating call
							 } //end of response text check
						 } //end of loop

					 } //end of Ajax connectivity check
				 } //end of Ajax ready state
			 }; //end of function for execution on ready state change

error line-> var data = 'currentUsername=' + encodeURIComponent(currentUsername) + '&currentEntityId =' + encodeURIComponent(currentEntityId)
					 + '&starNumber =' encoreURIComponent(starNumber);
		 ajax.open('POST', 'resources/star_submit.php?' + data, true);
		 ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

}//End of verifyUsername() function
Link to comment
Share on other sites

Generally, that error comes about when there is a mismatch with your curly braces. In other words, you're probably missing a curly brace, semicolon or some other essential grammatical structure somewhere.


I'd start commenting out parts of your code until you can localize the error.

Good luck.

  • Upvote 1
Link to comment
Share on other sites

Found two of the problems. First of all one of my encodeURIComponent functions is misspelled encoreURIComponent, and I am missing the concatenation operator on that last encodeURIComponent.There still seems to be some other issues so I'm digging in deeper for that. JSLint / JSHint have been a help and I will utilize them in the future.

Link to comment
Share on other sites


  • Create New...