Generate randon string using javascript,ajax and php

How to generate a random string using character, numbers and symbols. generate randon string using javascript, ajax and php with character,symbols, numbers and with specific length of string also, can create lenght of string as required to generated a string.

Create a index.php and this code  and you can download the jquery here to include to work ajax function




   
 <html>
<head>
<title>Random string code Generator using javscript, ajax and php</title>

 <script src="/jquery-2.1.4.min.js"></script>
 
 <script type="text/javascript">
   

function getrandonstringcode() {
         var length = 8;
       $.ajax({    

     url: "string.php", 

     method: 'post',

     data: {type:'string_action',length: length},

     dataType: 'html',

     success: function(response){

       response=response.replace(/(\r\n|\n|\r)/gm,"");
         $('#p_code').val(response);
          

     }

     });

}

</script>

</head>
<body>
<h1>Generate Random String using characters,numbers and symbols</h1>
          <input class="form-control" type="text" id="p_code" name="p_code" value="" placeholder="RandomString"/>
          <button type="button" onclick="getrandonstringcode()"  class="btn btn-success">Generate</button>

</body>
</html>
 Create a string.php file and add this code



   
 <?php  
   
   //only character or number only 
      if($_POST['type'] == 'string_action'){

       
      $length         = $_POST['length'];
        $useLetters     =  true;
        $useNumbers     =  true;
        $useSymbols     =  false;
      
        $uppercase    = ['Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'Z', 'X', 'C', 'V', 'B', 'N', 'M'];
        $lowercase    = ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'];
        $numbers      = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        $symbols      = ['`', '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '\\', '|', '/', '[', ']', '{', '}', '"', "'", ';', ':', '<', '>', ',', '.', '?'];

        $characters   = [];
        $coupon = '';

        if ($useLetters) {
            if ($useMixedCase) {
                $characters = array_merge($characters, $lowercase, $uppercase);
            } else {
                $characters = array_merge($characters, $uppercase);
            }
        }

        if ($useNumbers) {
            $characters = array_merge($characters, $numbers);
        }

        if ($useSymbols) {
            $characters = array_merge($characters, $symbols);
        }
 
            for ($i = 0; $i < $length; $i++) {
                $randString .= $characters[mt_rand(0, count($characters) - 1)];
            }
     
     //print coupon and exit go to back to the index file  
       echo $randString;
     exit(0);

   

  }

?>  
and download jquery and include into the file



   <script src="/jquery-2.1.4.min.js"></script>
 

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...