生成随机密码.按钮插入文本框


generate a random password. Button into a textbox

我有一个向网站添加新用户名和密码的表单。我正在尝试添加一个按钮,它可以生成一个12个字符的密码,并将其放入密码文本框中。每次按下此按钮时,都应该删除已输入的密码,并添加另一个12个字符的密码。

这是我的布局:http://snag.gy/L4woo.jpg

以下是我的表格:

<form method="post" action="addNewLogin.php" name="addUserLoginForm" id="addUserLoginForm" onsubmit= "return validateNewLoginForm()" >
    <input type="hidden" name="submitAttempt" value="true"/>
    <table style="background-color: White; padding:20px;" align="center">
                    <tr>
            <td style="width:180px;">
                        <label style="margin-left:400px;">Username</label>
                    </td>
                    <td style="width:420px;">
                                <input name="username" type="text" style="width:140px;" onchange= "return usernameValidation(this)" />
                    </td> 
        </tr>
        <tr>
                    <td>
                        <label style="margin-left:400px;">Password</label>
                    </td>
                    <td>
                        <input name="password" type="password" style="width:140px;" onchange= "return passwordValidation(this)"/>
                    </td>
                            <td style="margin-right: 200px;">
                                <button type="password" type= "text" onclick="return generateKey()">Generate Password!</button>
                            </td>
        </tr>
        </table>
<div align="center"><input type="submit" value="Add Login Details For New User" /></div>
</form>

我的用户名和密码通过使用标准regex 的单独JavaScript函数进行验证

这是我的12字符生成密码功能:

public function generateKey()
{
        $random_bytes = openssl_random_pseudo_bytes(9);
        $random_string = mysql_escape_string(str_replace(array(1 => "+",2 => "/"), array(1 => "-", 2 => "_"), base64_encode($random_bytes)));
        return $random_string;
}

我不知道如何单击此按钮将此功能应用于所需的文本框。希望你能帮忙。感谢

为什么要在服务器上生成新密码
只需使用javascript在客户端生成密码!

编辑:下面是一个关于如何使用JS和jQuery 的快速示例

http://jsfiddle.net/kannix/KhWR4/

并且请重构您的html代码<button type="password"是无效的html;)

如果我理解正确,这应该用jquery和ajax来完成。每次单击后,都会将php生成的新密码加载到输入元素中。但是您应该将id添加到元素中。然后类似:

$('#idofbutton').live("click", (function(){         
        // Ajax
        $.ajax({
            type: "GET",
            async: false,
            url: "yourphpwithgenerateKey_Codeonly.php",
            data: "",
            success: function(data){                
                $('#idofpasswordinput').text(data);                     
            }   
        });// $.ajax        
    })); 

在本例中,php文件中只有密钥代码。当然,你可以通过get parameters来处理一个大php文件的单个方法,让php知道该使用哪种方法。希望你理解我的意思。