我有一个表单添加一个新的用户名和密码到一个网站。我正在尝试添加一个按钮,它可以生成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;
}我不知道如何单击此按钮将此函数应用于所需的文本框。希望你能帮上忙。谢谢
发布于 2012-09-07 17:56:57
为什么要在服务器上生成新密码?
只需使用javascript在客户端生成您的密码!
编辑:这里有一个快速示例,说明如何使用JS和jQuery来实现这一点
http://jsfiddle.net/kannix/KhWR4/
并且请重构您的html代码<button type="password"是无效的html ;)
发布于 2012-09-07 18:05:08
如果我理解正确的话,这应该用jquery和ajax来完成。每次单击之后,都会将一个由php生成的新密码加载到input元素中。但是您应该向元素添加ids。然后像这样:
$('#idofbutton').live("click", (function(){
// Ajax
$.ajax({
type: "GET",
async: false,
url: "yourphpwithgenerateKey_Codeonly.php",
data: "",
success: function(data){
$('#idofpasswordinput').text(data);
}
});// $.ajax
})); 在本例中,只在php文件中包含关键代码。当然,您可以通过get-参数来处理大型php文件的单个方法,让php知道要使用哪个方法或其他方法。希望你能理解我的意思。
https://stackoverflow.com/questions/12315755
复制相似问题