1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Disable users from submitting the same ID more than once

Discussion in 'HTML & JavaScript' started by AnonLeo, Jan 17, 2014.

Tags:
  1. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    Hey,

    So I have a form that users put an ID (e.g: 89123) and then they click submit, but there are some people who submit the SAME ID more than once and that makes the server a little slower, is there a way to not allowing them to submit the same ID more than one time?

    In other words; Let's say a user writes "99432" at 9:00PM in the <input> of the form, he clicks "Submit" and then the process is done
    He wants to submit the SAME ID (99432) again in the form at 9:10PM and then he clicks "Submit" and and then the process is done

    What I want is a PHP code or something to disable the users from submitting the SAME ID if it's already submitted before


    I hope I made myself clear :D Sorry for poor English
    Thanks and I hope someone will help me with this problem :D
     
  2. Jeff Bezos

    Jeff Bezos Junior Member

    Joined:
    Jan 11, 2014
    Messages:
    178
    Likes Received:
    81
    ID for what? Loop through the table where the IDs are stored and if that ID exists, throw an error, if not allow the ID.
     
  3. Jeff Bezos

    Jeff Bezos Junior Member

    Joined:
    Jan 11, 2014
    Messages:
    178
    Likes Received:
    81
    I lied, you shouldn't need to loop. Just do a select query with the textbox value from that table and if it !=="" then allow the entry.
     
  4. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    I think what you should do is to create a cookie with the login time as value.

    Then, whenever the submit button is clicked, before submitting the form, the javascript will check the cookie, compare the time, then decide whether to allow submitting.

    Added:
    If you want to enable multiple id, just make sure the value stored in the cookie is unique to each id.
    e.g.
    [id1]-time
    [id2]-time
     
  5. Herion

    Herion Jr. VIP Jr. VIP Premium Member

    Joined:
    Jul 8, 2012
    Messages:
    289
    Likes Received:
    73
    The two main ways would be:

    MySQL + UNIQUE constraint for the column + regular INSERT statement.
    Cookies/IP/Browser Hashing (if you don't want the same user submitting the same ID).
     
  6. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    What is the full code for that? (if you know) :D
     
  7. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    do you know the code for that? PHP? :D
     
  8. 0_00_0

    0_00_0 Senior Member

    Joined:
    Oct 7, 2010
    Messages:
    1,024
    Likes Received:
    486
    Location:
    Canada
    Here you go. Something like this should work. This is obviously after you have connected to SQL and selected the database though.

    Code:
    $result = mysql_query("SELECT 1 FROM teh_table WHERE `ID` = '$formID'");
    if ($result && mysql_num_rows($result) > 0){
            echo 'ID Exists already!'; 
    }else{
        echo 'ID Doesn't exist or there is a database connection issue. Error: ' . mysql_error();
    }
    
     
  9. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    8,891
    Likes Received:
    7,493
    Occupation:
    ZLinky2Buy SEO Services
    Location:
    ⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩⇩
    Home Page:
    SELECT COUNT(*) AS howmany FROM WHEREVER WHERE id = "$id"

    Fetch the results. If row['howmany'] > 0 then disallow_signup()
     
  10. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    I have tried it but id doesn't seem to work :/
     
  11. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    Firstly, my interpretation of your question is you are trying to prevent the traffic from the user if the same id has been submitted for the last hour.

    The difference with this is that this assumption should let us work towards preventing the submit and not after submitting, check if the submit is a duplicate one.

    The below code is partly copied from hxxp://www.quirksmode.org/js/cookies.html
    Thus, the credit goes to the original content creator

    What I am doing is to check if the id has been set in the cookie?
    If yes, that means it has been submitted and prevent the submission.
    If not, set a cookie for the id that expires in 1 hour, and proceed with the submission.

    Code:
    <html>
    	<head>
    		<script src="http://code.jquery.com/jquery-latest.min.js"></script>
    		
    		<script>
    		function createCookie(name,value) {
    			var date = new Date();
    			date.setTime(date.getTime()+(1*60*60*1000));
    			var expires = "; expires="+date.toGMTString();
    
    			document.cookie = name+"="+value+expires+"; path=/";
    		}
    
    		function readCookie(name) {
    			var nameEQ = name + "=";
    			var ca = document.cookie.split(';');
    			for(var i=0;i < ca.length;i++) {
    				var c = ca[i];
    				while (c.charAt(0)==' ') c = c.substring(1,c.length);
    				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    			}
    			return null;
    		}
    
    		function eraseCookie(name) {
    			createCookie(name,"",-1);
    		}
    
    		jQuery(document).submit(function(event, data) 
    		{ 
    			var c = readCookie(jQuery("#id").val());
    		
    			
    			if (null == c) {
    				createCookie(jQuery("#id").val(), 'set');
    			} else {
    				alert("you have submitted this id within the last hour");
    				return false;
    			}
    		}); 
    
    		</script>		
    	</head>
     
    	<body>
    		<form action="targeturl" method="POST" ><br/>
    			id : <input type="id" id='id' value="1" /><br/>
    
    			<input type="submit" value="Submit" />
    		</form>
    		
    
    	</body>
    </html>
    
     
    • Thanks Thanks x 1
  12. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    Hi, what if I want to change it from 1 hour to 3 minutes?
     
  13. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    If you want 3 minutes, basically, you just need to increase the date time setting
    date.setTime(date.getTime()+(1*60*60*1000));
    to
    date.setTime(date.getTime()+(3*60*60*1000));

    to increase it to days, e.g. 2 days
    date.setTime(date.getTime()+(2*24*60*60*1000));

    hth
     
    • Thanks Thanks x 1
  14. AnonLeo

    AnonLeo Regular Member

    Joined:
    Nov 23, 2013
    Messages:
    362
    Likes Received:
    263
    Location:
    USA
    I hope you don't mind me asking; How can I make a timer for the form when a user submits the form?
    Explanation: Let's see a visitor comes to my website, he writes his ID and clicks submit, the process is done, now he wants to go back and submit the form again (Same ID or different ID sometimes), he submits again after 30 seconds. What I want is a code that will tell him "Please wait 3 minutes before submitting the form again" Instead of "You have submitted within the past hour".
    Is there a JS code or something for that? :)
    I hope you understand what can of code I am talking about :D
    Thanks in advance
     
  15. mypmmail

    mypmmail Junior Member

    Joined:
    Jan 31, 2008
    Messages:
    111
    Likes Received:
    27
    What you can do is to change the statement below

    alert("you have submitted this id within the last hour");

    to whatever message you wished to be prompt.

    In this particular case, it will be
    alert("Please wait 3 minutes before submitting the form again");

    On a side note, you would surely want your message to tally with the length of time user should wait for.
    i.e.
    if you script set to check user to wait for 30 minutes, your message should state 30 minutes.

    hth
     
    • Thanks Thanks x 1