here if i click the Generate Employee Id button it should generate the random number in input textfield and then that button should hide after generate the id. how to do that? but, i can automatically generate the random number in input textfiled without the button. that code is below i'm posting. i want to use button here.. any help...??

<td><input type="text" name="emp" readonly="readonly" value="" /></td>
<td><input type="button" value="Generate Employee Id" onclick="" /></td>

this is the code for generate auto random number in input textfield without button..

<input type="text" name="emp" readonly="readonly" value="<?php 
    function gen_random_string($length=6)
    {
        $chars ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        $final_rand ='';
        for($i=0;$i<$length; $i++)
        {
            $final_rand .= $chars[ rand(0,strlen($chars)-1)];
        }
        return $final_rand;
    }
    echo gen_random_string()."\n"; //generates a string 
    ?>" />

Recommended Answers

All 13 Replies

This should work:

<script>
function randomStringToInput(clicked_element)
{
    var self = $(clicked_element);
    var random_string = generateRandomString(10);
    $('input[name=emp]').val(random_string);
    self.remove();
}

function generateRandomString(string_length)
{
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var string = '';

    for(var i = 0; i <= string_length; i++)
    {
        var rand = Math.round(Math.random() * (characters.length - 1));
        var character = characters.substr(rand, 1);
        string = string + character;
    }

    return string;
}
</script>

<td><input type="text" name="emp" readonly="readonly" value=""></td>
<td><input type="button" value="Generate Employee Id" onclick="randomStringToInput(this)"></td>

What it does is it uses Javascript to generate a random string of X length, and then sets the value of input[name=emp] to be that random string.

not working..

perhaps you are good at code, so unfortunately I do not understand your problem

Well, it is working for me here, so then I must have falsely assumed you are working with jQuery. If you are working with jQuery, there must be something else going wrong. Did you check your Firebug for Javascript errors?

If you are NOT using jQuery, changing the randomStringToInput() function to this should work:

function randomStringToInput(clicked_element)
{
    var random_string = generateRandomString(10);
    var element = document.getElementsByName('emp')[0];
    element.value = random_string;
    clicked_element.parentNode.removeChild(clicked_element);
}

@ minitauros : superb thanks.. now its working... i have one more doubt. if i want a random number like this HBR_EMP_001, HBR_EMP_002, HBR_EMP_003. the number should increase one by one. how to do that? i need to get some unique auto generate number...

@ cholonq6 : lol. if i'm good at code why i can ask these kind of silly questions? you are a humorous person... :-)

Well, for starters, HBR_EMP_001 doesn't sound pretty random to me ;). If you want to increase a known number-letter combination by one, you could try this code in PHP (and note that this is just an example, working with the idea that your strings will always look like 'HBR_EMP_(number)'):

<?php
$string = 'HBR_EMP_001';
$regex = '/^\w+(\d+)$/';
preg_match($regex, $string, $matches);
$last_used_number = $matches[1];
$new_number = $last_used_number++;
$new_string = 'HBR_EMP_' . (str_pad($new_number, 3, 0, STR_PAD_LEFT));

its not working. leave this matter. in that previous code

generateRandomString(10)

we mentioned 10. but it shown 11 characters. if i mention generateRandomString(2) it shown 3 characters. why?

Oh that is because I seem to have made a typo in the for loop construction. It should be for(var i = 0; i < string_length; i++) instead of for(var i = 0; i <= string_length; i++). Hope this helps :)!

PS: I seem to have also made a small mistake in the other PHP script. It should be $new_number = $last_used_number + 1; instead of $new_number = $last_used_number++;.

thanks @ minitauros. but php code does not work..:-(

This one is working for me though:

<?php
$string = 'HBR_EMP_001';
echo $string; // Outputs HBR_EMP_001
$regex = '/^\w+(\d+)$/';
preg_match($regex, $string, $matches);
$last_used_number = $matches[1];
$new_number = $last_used_number + 1;
$new_string = 'HBR_EMP_' . (str_pad($new_number, 3, 0, STR_PAD_LEFT));
echo $new_string; // Outputs HBR_EMP_002

:o

Good day. i am working on a project and i am new to this.if anyone has a tutorial on how to generate certain total of random number and store them in db without repeating and print the number on a page.Your idea is highly appreciated.

I creat a table in html

<form name="form1" method="post" action="">
  <p>Code length:        
<label for="length"></label>
    <select name="length" id="length">
      <option>Select</option>
      <option>12</option>
    </select>
  </p>
  <p>Code type:
    <label for="codetype"></label>
             
    <select name="codetype" id="codetype">
      <option selected>Select</option>
      <option>Numeric</option>
    </select>
  </p>
  <p>Number of pin:
    <label for="nop"></label>
    <input type="text" name="noc" id="noc">
  </p>
  <p>
    <input type="submit" name="button" id="button" value="Generate Code">
  </p>
  <p> </p>
</form>

config file

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="nikolai"; // Mysql password
$db_name="uba"; // Database name
$tbl_name="pin"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>

function generateRandomString(string_length)
{
    var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
    var string = '';
    for(var i = 0; i <= string_length; i++)
    {
        var rand = Math.round(Math.random() * (characters.length - 1));
        var character = characters.substr(rand, 1);
        string = string + character;
    }
    return string;
}
</script>

<input type="button" value="Generate Employee Id" onclick="document.getElementById ('Temptext').value=generateRandomString(10)">

This Will Work For Sure

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.