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

Have a JS random number coding question for the coders.

Discussion in 'HTML & JavaScript' started by Oblivion13, Dec 10, 2012.

  1. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    Basically, I am wanting something to generate a unique 5 digit ID, like Ah45P or something along those line.

    Heres my set up, someone comes to the site, fills out a form, they go to a thank you page, then they see "click to get your Unique ID", upon click they go to another page that says.. "Your Unique ID is 12345" . (hope that made sense)

    Is there any tutorials that I can do that with?
     
  2. Abstroose

    Abstroose Elite Member

    Joined:
    Nov 20, 2008
    Messages:
    2,097
    Likes Received:
    3,475
    Occupation:
    Thai Boxer
    Location:
    UK
    Home Page:
    Does the code actually have to be unique and able to identify the user?
     
  3. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    It dont need to be able to identify the user, Its more less a dummy code.
     
  4. saxgod

    saxgod Regular Member

    Joined:
    Sep 19, 2010
    Messages:
    351
    Likes Received:
    337
    here you go, its php based though
    and it uses shrinkonce as contentlocker

    index.php
    PHP:
    <?php
    session_start
    ();
    $_SESSION['token'] = md5(uniqid("sax"true));
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"https://shrinkonce.com/api.php?key=<yourapikeyhere>&action=shrink&url=http://www.domain.com/ticket.php?token=".$_SESSION['token']);
    curl_setopt($chCURLOPT_HEADER0);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    $url curl_exec($ch);
    $shrinkonce explode("=",$url);
    $shrinkonce $shrinkonce[1];
    ?>

    <html>
       <head>
         <title>Lottery</title>
         <link rel="stylesheet" type="text/css" href="https://shrinkonce.com/css/widget.css" />
         <script src="https://shrinkonce.com/js/shrinkonce.js"></script>
       </head>
       <body>
             Instant win, blabla<br/><br/>
             <a href="#" onClick="javascript:shrinkonce_earn('<?php echo $shrinkonce ?>');">Get your ticket!</a>
             <br/><br/>
             <a href="ticket.php?token=<?php echo $_SESSION['token']?>">Demo: Without CL. This should not be in the live version!</a>
             <div id='shrinkonce_survey' class='modal hide' style='text-align:center; background-color: #888; border: none; overflow:hidden;'></div>
             </body>
    </html>
    ticket.php:
    PHP:
    <?php
    session_start
    ();
    if (!isset(
    $_GET['token']))
            
    header("Location: index.php");
    if (!isset(
    $_SESSION['token']))
            
    header("Location: index.php");
    if (
    $_GET['token'] != $_SESSION['token'])
            
    header("Location: index.php");

    $n rand(10000,99999);
    unset(
    $_SESSION['token']);?>
    <html>
       <head>
         <title>Lottery</title>
       </head> 
       <body>
         <h1 style="text-align: center">Your lottery ticket number:</h1>
         <div id="ticket" style="text-align: center; font-size: 18pt; color: blue; position: relative; top: 200px;">
            <?php echo $n ?>
         </div>
       </body>
    </html>
     
    • Thanks Thanks x 1
    Last edited: Dec 10, 2012
  5. Abstroose

    Abstroose Elite Member

    Joined:
    Nov 20, 2008
    Messages:
    2,097
    Likes Received:
    3,475
    Occupation:
    Thai Boxer
    Location:
    UK
    Home Page:
    Code:
    function makeid(){
        var text = "";
        var possible = "abcdefghijklmnopqrstuvwxyz0123456789";
    
    
        for( var i=0; i < 5; i++ )
            text += possible.charAt(Math.floor(Math.random() * possible.length));
    
    
        return text;
    }
    Code:
    document.write("Your Unique ID is "+makeid());
     
    • Thanks Thanks x 1
  6. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,062
    Likes Received:
    2,872
    Gender:
    Male
    Make an AJAX call to a page on click where you generate the ID with PHP (or equivalent). Having said that, a 5 digit "number" is not really unique. Better try to generate something bigger, with more charsets e.g. the alphabets and more.
     
  7. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    Where would I put this in the html?

    Code:
    document.write("Your Unique ID is "+makeid());
    Heres is where its gonna go.

    Code:
    <h1>Congratulations  </h1>
    <p style="margin-left:45px">Your unique ID is : </p>
     
  8. Gogol

    Gogol Elite Member

    Joined:
    Sep 10, 2010
    Messages:
    3,062
    Likes Received:
    2,872
    Gender:
    Male
    u need jquery for this though..
    Code:
    <h1>Congratulations  </h1>
    <p style="margin-left:45px">Your unique ID is : <span id="abc"></span>
    <script>
    jQuery(function($){$('#abc').html(makeid());});
    </script>
    
     
    Last edited: Dec 10, 2012
  9. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    Thanks for eveyones help, I was able to get it with.

    Code:
       [FONT=arial] <p>Your unique ID is: <span id="uniqueID"></span></p>[/FONT]
    
    
    
    [FONT=arial]    <script>[/FONT]
    
    [FONT=arial]      function makeid(){[/FONT]
    
    [FONT=arial]        var text = "";[/FONT]
    
    [FONT=arial]        var possible = "[/FONT][FONT=arial]ABCDEFGHIJKLMNOPQRSTUVWXYZabcd[/FONT][FONT=arial]efghijklmnopqrstuvwxyz01234567[/FONT][FONT=arial]89";[/FONT]
    
    [FONT=arial]        for( var i=0; i < 5; i++ ){[/FONT]
    
    [FONT=arial]          text += possible.charAt(Math.floor([/FONT][FONT=arial]Math.random() * possible.length));[/FONT]
    
    [FONT=arial]        }[/FONT]
    
    [FONT=arial]        return text;[/FONT]
    
    [FONT=arial]      }[/FONT]
    
    
    
    [FONT=arial]      document.getElementById("[/FONT][FONT=arial]uniqueID").innerHTML = makeid();[/FONT]
    
    [FONT=arial]    </script>[/FONT]    
     
  10. jazzc

    jazzc Moderator Staff Member Moderator Jr. VIP

    Joined:
    Jan 27, 2009
    Messages:
    2,468
    Likes Received:
    10,143
    Thread moved to the proper place :)
     
    • Thanks Thanks x 1
  11. Oblivion13

    Oblivion13 Regular Member

    Joined:
    Sep 7, 2011
    Messages:
    459
    Likes Received:
    247
    Thanks Jazz, I couldnt find a coding section when I made the thread.