Reputation: 273
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
Upvotes: 0
Views: 3197
Reputation: 303
When I understand right this should be done with jquery and ajax. After each click, you load a new php-generated password into the input element. But you should add ids to the elements. Then sth like:
$('#idofbutton').live("click", (function(){
// Ajax
$.ajax({
type: "GET",
async: false,
url: "yourphpwithgenerateKey_Codeonly.php",
data: "",
success: function(data){
$('#idofpasswordinput').text(data);
}
});// $.ajax
}));
In this example you have the key code alone in a php file. Of course, you can address a single method of a big php file via get-parameters to let php know which method to use or so. Hope you understand what I mean.
Upvotes: 1
Reputation: 5157
Why do you generate the new password on your server?
Simply generate your password client-side with javascript!
Edit: Here is a quick example on how to do that with JS and jQuery
http://jsfiddle.net/kannix/KhWR4/
and please refactor your html code <button type="password"
is non valid html ;)
Upvotes: 1