Reputation: 25
I am generating passwords automatically by using php. I stored the generated password in a variable. Here i have assign those generated passwords to a text box by using ONCLICK property on button(button name is "Generate Password"). But i am not getting required output(password must be visible in textbox by clicking that button). Here i have attached respective code. Can anyone tell what i had done wrong??
<!DOCTYPE html>
<html>
<head>
<title>create_profile</title>
<link rel="stylesheet" type="text/css" href="create_profile.css" />
<script type="text/javascript">
function getPassword(){
document.getElementById("password").value="<?php
// Characters to use for the password
$str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// Desired length of the password
$pwlen = 8;
// Length of the string to take characters from
$len = strlen($str);
// RANDOM.ORG - We are pulling our list of random numbers as a
// single request, instead of iterating over each character individually
$uri = "http://www.random.org/integers/?";
$random = file_get_contents(
$uri ."num=$pwlen&min=0&max=".($len-1)."&col=1&base=10&format=plain&rnd=new"
);
$indexes = explode("\n", $random);
array_pop($indexes);
// We now have an array of random indexes which we will use to build our password
$pw = '';
foreach ($indexes as $int){
$pw .= substr($str, $int, 1);
}
echo $pw;
?>"; }
</script>
<h1> Create Profile </h1>
<label for="name">name:</label><br />
<input id="name" name="name" type="text" size="30" /><br />
<label for="user_id">user id:</label><br />
<input id="user_id" name="user_id" type="text" size="30" /><br />
<label for="email">Your Email:</label><br />
<input id="email" name="email" type="text" size="60" /><br />
<label for="password">password:</label><br />
<input id="password" name="password" size = "10" /> <br/>
<button onclick="getpassword();" >Generate Password</button>
Upvotes: 0
Views: 12372
Reputation: 1466
The correction of your suggestion
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ab</title>
<script type="text/javascript">
<?php $pw="hello"; ?>
// You've written here getPassword()
function getpassword(){
document.getElementById("password").value="<?php echo $pw; ?>";
}
</script>
</head>
<label for="password"> password: </label><br />
<input id="password" name="password" size = "10" />
<!-- there is no need for a semicolon here -->
<button onclick="getpassword()" >Generate Password</button>
<body>
</body>
</html>
without php:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>ab</title>
<script type="text/javascript">
// You've written here getPassword()
function getpassword(){
document.getElementById("password").value="hello";
}
</script>
</head>
<label for="password"> password: </label><br />
<input id="password" name="password" size = "10" />
<!-- there is no need for a semicolon here -->
<button onclick="getpassword()" >Generate Password</button>
<body>
</body>
</html>
Upvotes: 1
Reputation: 199
first-time learn javascript and jquery(it is javascript library) :) if you use jquery
<button onclick="function(){ $.get('/url/',function(dataReturnFromServer){alert(dataReturnFromServer);})}"></button>
Upvotes: 0