0

I have a form for adding a new username and password to a site. I'm trying to add a button which generates a 12character password and puts it into the password text box. Everytime this button is pressed it should delete the password in already and add in another 12 char password.

This is my layout: http://snag.gy/L4woo.jpg

Here is how my form:

<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>

My usernames and password validate through a separate JavaScript function using standard regex

And here is my 12character generate password function:

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;
}

I have no idea how to click on this button to apply this function to the required textbox. hope you can help. thanks

4

2 回答 2

1

当我理解正确时,应该使用 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知道使用哪个方法左右。希望你明白我的意思。

于 2012-09-07T10:05:08.017 回答
1

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

编辑:这是一个关于如何使用 JS 和 jQuery 做到这一点的快速示例

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

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

于 2012-09-07T09:56:56.737 回答