javascript - Displaying text in input field when a button is clicked(php/html) -
i need help following; want display newly generated password(function in php)when button "gww" clicked within input field. shed light?
<td><input type="button" value="generate" name="gww"></td> <td><input type="text" id="ww"></td> <script type="text/javascript"> function password() { var elem = document.getelementbyid("ww"); elem.value = "<? echo random_password(); ?>"; } </script> function random_password($length = 8) { //empty password string $password = ""; //possible characters $karakters = "123456789abcdefghijklmnpqrstuvwxyz"; $maxlength = strlen($karakters); if($length > $maxlength) { $length = $maxlength; } $i = 0; while($i < $length) { $char = substr($karakters, mt_rand(0, $maxlength-1), 1); if(!strstr($password, $char)) { $password .= $char; $i++; } } homecoming $password; }
i suspect want new password shown whenever click button. have utilize ajax this.
so have javascript function:
function generatepassword() { var xmlhttp; if (window.xmlhttprequest) { // code ie7+, firefox, chrome, opera, safari xmlhttp=new xmlhttprequest(); } else { // code ie6, ie5 xmlhttp=new activexobject("microsoft.xmlhttp"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4 && xmlhttp.status==200) { document.document.getelementbyid("ww").value = xmlhttp.responsetext; } } xmlhttp.open("get","http://www.example.com/generatepassword.php",true); xmlhttp.send(); }
your html should be
<input type="button" value="generate password" onclick="generatepassword()">
generatepassword.php:
<?php echo random_password(); function random_password($length = 8) { //your function here ; } ?>
php javascript html input
No comments:
Post a Comment