AaronHatton AaronHatton - 5 months ago 61
PHP Question

PHP Password Generator Form within Form

I have found a password generator and would like to have a form which allows a user to register but if they wish to generate a password they can press Generate password and it fills in a text input box with the password.

I then want them to be able to press Register and it sends all of the form data with either the manually set password or the generated password to the form submission.

Any ideas how I can put the form within a form with it being valid HTML?

My code is as follows:

<form id="register" action="registerprocess.php" method="post">
<span><strong>Please note all fields are required</strong></span><br /><br />
<label for="fullname">Full Name: </label><input type="text" id="fullname" name="fullname" required><br />
<label for="email">Email Address: </label><input type="email" id="email" name="email" required><br /><br />
<label for="manualpass">User-Defined Password: </label><input type="text" id="manualpass" name="manualpass" required><br />
<?php

$f_contents = file("..\sec\possiblewords.txt");
$word = $f_contents[rand(0, count($f_contents) - 1)];

function vowelreplacement() {
global $word;
$string = $word;
$trans = array("a" => "4", "e" => "3", "i" => "1", "o" => "0");

return strtr($string, $trans);
}

$generatedpassword = vowelreplacement();

?>
<form id="vowelremover" method="post">
<label for="generatedpass">Generated Password: </label><input type="text" id="generatedpass" name="generatedpass" value=<?php echo $generatedpassword;?> required><input type="submit" value="Generate Password">
</form>
<input type="submit" value="register">
</form>

Answer

Implement jQuery Plugin and then do the following:

ajax.php (e.g. on server test.com)

<?php

$f_contents = file("..\sec\possiblewords.txt");
$word = $f_contents[rand(0, count($f_contents) - 1)];

function vowelreplacement() {   
    global $word;
    $string = $word;
    $trans = array("a" => "4", "e" => "3", "i" => "1", "o" => "0");

    return strtr($string, $trans);
}           

echo vowelreplacement();

form.php (php file where you place your form)

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

<form id="register" action="registerprocess.php" method="post">
    <span><strong>Please note all fields are required</strong></span><br /><br />
    <label for="fullname">Full Name: </label><input type="text" id="fullname" name="fullname" required><br />
    <label for="email">Email Address: </label><input type="email" id="email" name="email" required><br /><br />
    <label for="manualpass">User-Defined Password: </label><input type="text" id="manualpass" name="manualpass" required><br />

    <form id="vowelremover" method="post">          
        <label for="generatedpass">Generated Password: </label><input type="text" id="generatedpass" name="generatedpass" value=<?php echo $generatedpassword;?> required><input type="submit" value="Generate Password">
    </form>
    <input type="submit" value="register">
</form>

<script type="text/javascript">

$(function() {

    $("#vowelremover").submit(function() {

        $("#generatedpass").load("http://test.com/ajax.php");

        return false;
    });
});

</script>