...

View Full Version : Preventing a user to click a second time



angelali
05-23-2012, 07:48 PM
Hello, I'm making a basic rating system in CSS and JavaScript.. I want that when a user has clicked on the button, he cannot click again on it after that, how to prevent this? If possible with JQuery is welcome..

Hashim1
05-23-2012, 08:05 PM
My knowledge of JavaScript is fairly basic, so if anyone has got a better way to do this, which no doubt there will be, feel free to post it.

I see this as a fairly simple script, and here's what I've got:

JavaScript code:



function disableButton() {
document.getElementById("button1").disabled = true;
}

HTML code:



<body>
<input type="submit" id="button1" onClick="disableButton()"/>
</body>
</html>Just a single JavaScript statement which changes the button element's disabled value to true, therefore disabling it. See if that works how you want it to. As already mentioned, my knowledge of JavaScript is very little, and someone else will probably be able to do this much better than me.

Philip M
05-23-2012, 08:06 PM
Hello, I'm making a basic rating system in CSS and JavaScript.. I want that when a user has clicked on the button, he cannot click again on it after that, how to prevent this? If possible with JQuery is welcome..

You do not need the sledgehammer of jQuery to crack this small nut!


<input type = "button" id = "mybutton" value= "You May Click this Button Once Only" onclick = "this.disabled = true">

Remember that the user can simply press F5 and reload the page! To stop that you would have to use a cookie to record that the button had been pressed - which the user can also delete. I would think this one through! ;)

Hashim1 - a submit button does what it says - it submits a form to a server-side script. Once the form has been submitted the page will reload.

He has won six million dollars in as many years. - Presenter, Channel 5

angelali
05-23-2012, 08:08 PM
That's i want, the cookie things.... I did not know how to explain this above in my question

felgall
05-23-2012, 08:12 PM
They could also turn off JavaScript and then click lots of times - or simply use their browser privacy mode that disables JavaScript's ability to write cookies.

To prevent their subsequent clicks being counted you'd need to do something server side - perhaps by collecting the ip address and discarding additional clicks made from that ip address in the next so many minutes after the first one. You wouldn't want to block it entirely as the ip may be being shared by hundreds of people in an office or swapped around by the ISP so that different people are using it at different times.

Philip M
05-23-2012, 08:13 PM
That's i want, the cookie things.... I did not know how to explain this above in my question

But the user can easily delete the cookie! Or disable Javascript!

The "cookie things" have been covered a zillion times in this forum - try using the search feature. But you may find cookies are too advanced for you at this stage.

felgall's suggestion would probably work, but IP adresses are often dynamic and change every time the user re-boots his router. You could block a second click in this way for some minutes, or even an hour or so, but not permanently.

As I say, I would think this one through.

angelali
05-23-2012, 08:14 PM
If like that, I better do this in PHP and store the rating in database..

Hashim1
05-23-2012, 08:42 PM
You do not need the sledgehammer of jQuery to crack this small nut!


<input type = "button" id = "mybutton" value= "You May Click this Button Once Only" onclick = "this.disabled = true">Remember that the user can simply press F5 and reload the page! To stop that you would have to use a cookie to record that the button had been pressed - which the user can also delete. I would think this one through! ;)

Hashim1 - a submit button does what it says - it submits a form to a server-side script. Once the form has been submitted the page will reload.

He has won six million dollars in as many years. - Presenter, Channel 5

Ah, ok, I see what you mean. So my script would have worked if the button was an ordinary button which didn't reload the page?

felgall
05-23-2012, 10:40 PM
So my script would have worked if the button was an ordinary button which didn't reload the page?

Then it would do nothing at all if JavaScript isn't enabled for the page.

Taro
05-23-2012, 10:53 PM
Hello,

You can try to learn from this (http://stackoverflow.com/questions/2658163/prevent-users-from-performing-an-action-twice) webpage. It uses server-side (PHP), client-side (JavaScript/ J SON/ J Query), and database-side (MySQL) scripting. It should be possible to incorporate the piece of code with the system.

Hashim1
05-24-2012, 12:18 AM
Then it would do nothing at all if JavaScript isn't enabled for the page.

Of course it wouldn't - to run a JavaScript code it's a given that you need to have it actually enabled. I'm asking whether the code I posted would be working client-side code, provided JavaScript is enabled?

felgall
05-24-2012, 03:11 AM
Of course it wouldn't - to run a JavaScript code it's a given that you need to have it actually enabled. I'm asking whether the code I posted would be working client-side code, provided JavaScript is enabled?

Well in that case you should be adding the button itself from the JavaScript so that those without JavaScript don't see a broken button.

angelali
05-24-2012, 09:36 AM
I'm doing it in PHP and MySQL...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum