Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have the following code that submits a rating to a php form, however I am able to click multiple times on the link to give lots of ratings from the same user, I want to disable clicks after the user has clicked once.

JavaScript
var clicked = false;

var ratingAction = {
	
		'a.rater' : function(element){
			element.onclick = function(){
			if (!clicked)
			{
				clicked = true;
			}
			else
			{
				alert('You can only rate once!');
				return false;
			}
			
			var parameterString = this.href.replace(/.*\?(.*)/, "$1"); // onclick="sndReq('j=1&q=2&t=127.0.0.1&c=5');
			var parameterTokens = parameterString.split("&"); // onclick="sndReq('j=1,q=2,t=127.0.0.1,c=5');
			var parameterList = new Array();

			for (j = 0; j < parameterTokens.length; j++) {
				var parameterName = parameterTokens[j].replace(/(.*)=.*/, "$1"); // j
				var parameterValue = parameterTokens[j].replace(/.*=(.*)/, "$1"); // 1
				parameterList[parameterName] = parameterValue;
			}
			var theratingID = parameterList['q'];
			var theVote = parameterList['j'];
			var theuserIP = parameterList['t'];
			var theunits = parameterList['c'];
			
			//for testing	alert('sndReq('+theVote+','+theratingID+','+theuserIP+','+theunits+')'); return false;
			sndReq(theVote,theratingID,theuserIP,theunits); return false;		
			}
			
        		
		
		
        
		
	};
Behaviour.register(ratingAction);


Here is how my link is written

PHP
$rater.='<li><a href="functions/db2.php?j='.$ncount.'&q='.$id.'&t='.$ip.'&c=5" title="'.$ncount.' out of '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';


I have a check in php that reads from the database to see if the user has made a rating already but this check only works if a user clicks, waits for the page to process and then tries to click again. Multiple clicks in a quick succession will submit multiple entries.
Posted

1 solution

Think about it: what's a click? :-)

Of course you cannot prevent the user to click on anything anytime, you can only process clicks on different ways depending on is it the first click or not. Isn't this obvious? I can give you just a simple idea on how to do it:

XML
<html>
<body>

<script type="text/javascript"><!--
var clicked = false;
function processClick() {
    if (clicked) return;
    clicked = true;
    alert("You can click many times, but this line will be executed only once");
}
--></script>

<p onclick="processClick();">Click here</p>

</body>
</html>


Simple, isn't it?

Of course, if the user reload the page, she or he can vote again. If you use session support, the user can close the session, open a new one ans still vote again. You can prevent it only if you have user authentication on server side, in PHP code. You would need a user account; the user will need to submit an authentication through a Web form to get a right to vote. If you want to do that, you will be able to allow only one vote per rating object per user account.

This is a separate topic. If you want that, there is plenty of manuals and PHP source code to deal with user account and authentication.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900