0

I've got a form, with just normal <input type="text"> its already in a form. But what I want to do, is having a randompassword generator to generate a password for the password field. The button needs to activate a script and put the value into the password field.

but its not possible to submit a form in a form right?

example code:

<?php
//password gen
if (isset($_POST['passgen'])) {
  $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  $generated_password = substr(str_shuffle($charset), 0, 12);   
}
//password gen end
?>

<form action="index.php" method="POST">
<input type="submit" name="submit-form-with-text" value="submit form"/> 
<input type="submit" name="activate-the-passgen" value="Generate a password"/>
<input type="text" value="<?php echo $generated_password; } ?>" size="15" /> 
</form>
5
  • 3
    use ajax for this purpose Commented Dec 30, 2013 at 11:00
  • 1
    My guess it that approximately zero users will use the option to generate a password, as they won't remember it and will choose to use a password they remember instead. Commented Dec 30, 2013 at 11:00
  • nested form is not possible but multiple forms is, store the generated pwd in the session and use it in the last submit, Ajax is a good option too Commented Dec 30, 2013 at 11:06
  • How do i call such option with ajax so i cna look it up? Commented Dec 30, 2013 at 11:07
  • @PhpRookie: I posted answer with jquery. Commented Dec 30, 2013 at 11:16

1 Answer 1

1

If you want use jquery then try it: Demo: http://jsfiddle.net/qsDn5/7/

JS

$(document).ready(function(){
  $("#genPass").click(function(){
        chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var result = '';
        for (var i = 15; i > 0; --i)
            result += chars[Math.round(Math.random() * (chars.length - 1))];
         $("#pass").val(result);
    })   
});

HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<form action="index.php" method="POST">
     <input type="submit" name="submit-form-with-text" value="submit form"/>
    <input type="button" id="genPass" name="activate-the-passgen" value="Generate a password"/>
    <input type="text" value="" id="pass" size="15" />
</form>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.