【问题标题】:generate a random password. Button into a textbox生成随机密码。按钮进入文本框
【发布时间】:2012-09-01 04:45:58
【问题描述】:

我有一个用于向站点添加新用户名和密码的表单。我正在尝试添加一个生成 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>

我的用户名和密码使用标准正则表达式通过单独的 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;
}

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

【问题讨论】:

    标签: php html forms login passwords


    【解决方案1】:

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

    编辑: 这是一个关于如何使用 JS 和 jQuery 实现此目的的快速示例

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

    请重构您的 html 代码 &lt;button type="password" 是无效的 html ;)

    【讨论】:

    • 这只是一个已经被使用的函数,我需要生成一个 60+ API 密钥来加入两个网站。认为密码很容易重用。显然不是。
    • 我将在我的答案中添加一个 jsfiddle,以举例说明如何在 js 中做到这一点
    【解决方案2】:

    当我理解正确时,应该使用 jquery 和 ajax 来完成。每次单击后,将一个新的 php 生成的密码加载到 input 元素中。但是您应该向元素添加 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知道使用哪个方法左右。希望你明白我的意思。

    【讨论】:

      猜你喜欢
      • 2010-09-08
      • 2012-11-06
      • 1970-01-01
      • 2022-11-14
      • 2015-04-13
      • 2012-07-28
      • 2011-09-05
      相关资源
      最近更新 更多