我使用php自动生成密码。我将生成的密码存储在变量中。在这里,我使用按钮上的ONCLICK属性将这些生成的密码分配给文本框(按钮名称为“生成密码”)。但我没有获得所需的输出(通过单击该按钮,密码必须在文本框中可见)。这里我附上了相应的代码。谁能说出我做错了什么?
<!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;
&GT;?“; }
</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>
答案 0 :(得分:1)
纠正你的建议
<!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>
没有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>
答案 1 :(得分:0)
第一次学习javascript和jquery(它是javascript库):) 如果你使用jquery
<button onclick="function(){ $.get('/url/',function(dataReturnFromServer){alert(dataReturnFromServer);})}"></button>