簡體   English   中英

生成隨機密碼。 按鈕進入文本框

[英]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>

我的用戶名和密碼使用標准正則表達式通過單獨的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生成的密碼加載到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知道使用哪種方法。 希望你明白我的意思。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM