Home > Software design >  how to print a specific text in html form on click of a button PHP?
how to print a specific text in html form on click of a button PHP?

Time:08-12

i am working on a php based password generator, i have a html form which has inputs for website name, Username and Passowrd. till now the user has to type the password so that it can get encrypted and stored , what i want is to have a button besides the password input form, which when clicked type the generated passoword in the password field.

this is the form i am using-

</head>

<body 
        <form  method="post">
            <!--Input Fields-->
            <input type="text" name="website" placeholder="Website" required autocomplete="off"> <br>
            <input type="text" name="username" placeholder="Username" required autocomplete="off"> <br>
            <input type="password" name="password" placeholder="Password" required autocomplete="off"> <br>
            <input type="submit" name="submit" value="SAVE" >
        </form>
    </div>

this is the function i want to get executed on press of the button-

 function password_generate($chars) 
        {
        $Uppercase="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        $Lowercase="abcdefghijklmnopqrstuvwxyz";
        $Numbers="1234567890";
        $Symbols="!@#$&* _?%";
        
          $data = $Uppercase.$Lowercase.$Numbers.$Symbols;
          return substr(str_shuffle($data), 0, $chars);
        }

CodePudding user response:

Using PHP and JavaScript you can achieve easily.

PHP code:

<body 
        <form  method="post">
            <!--Input Fields-->
            <input type="text" name="website" placeholder="Website" required autocomplete="off"> <br>
            <input type="text" name="username" placeholder="Username" required autocomplete="off"> <br>
            <input type="password" name="password" placeholder="Password" required autocomplete="off"> <br>
            <input type="button"  value="Generate" onClick="randomPassword(10);" tabindex="2"> <br>
            <input type="submit" name="submit" value="SAVE" >
        </form>
    </div>

JS Code:

<script>
function randomPassword(length) {
    var chars = "abcdefghijklmnopqrstuvwxyz!@#$%^&*()- <>ABCDEFGHIJKLMNOP1234567890";
    var pass = "";
    for (var x = 0; x < length; x  ) {
        var i = Math.floor(Math.random() * chars.length);
        pass  = chars.charAt(i);
    }
    myform.password.value = pass;
}
</script>

Hope this is working for you.

It's generate same random password as you mention in your PHP function.

CodePudding user response:

var pass = "(return substr(str_shuffle($data), 0, $chars));


  • Related