单击按钮时在输入字段中显示文本(php/html)
我需要以下方面的帮助;我想在另一个输入字段中单击按钮“gww”时显示新生成的密码(php中的函数)。有人能解释一下吗单击按钮时在输入字段中显示文本(php/html),php,javascript,html,input,Php,Javascript,Html,Input,我需要以下方面的帮助;我想在另一个输入字段中单击按钮“gww”时显示新生成的密码(php中的函数)。有人能解释一下吗 <td><input type="button" value="Generate" name="gww"></td> <td><input type="text" id="ww"></td> <script type="text/javascript"> function password()
<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++;
}
}
return $password;
}
函数密码()
{
var elem=document.getElementById(“ww”);
elem.value=“”;
}
函数随机密码($length=8)
{
//空密码字符串
$password=“”;
//可能的字符
$karakters=“123456789abcdefghijklmnpqrstuvwxyz”;
$maxlength=strlen($karakters);
如果($length>$maxlength)
{
$length=$maxlength;
}
$i=0;
而($i<$length)
{
$char=substr($karakters,mt_rand(0,$maxlength-1),1);
如果(!strstr($password,$char))
{
$password.=$char;
$i++;
}
}
返回$password;
}
我怀疑您希望在单击按钮时显示新密码。为此,您必须使用AJAX
因此,您有了javascript函数:
function generatePassword()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for 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();
}
你的HTML应该是
<input type="button" value="Generate password" onClick="generatePassword()">
generatePassword.php:
<?php
echo random_Password();
function random_Password($length = 8)
{
//your function here ;
}
?>
我怀疑您希望在单击按钮时显示新密码。为此,您必须使用AJAX 因此,您有了javascript函数:
function generatePassword()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for 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();
}
你的HTML应该是
<input type="button" value="Generate password" onClick="generatePassword()">
generatePassword.php:
<?php
echo random_Password();
function random_Password($length = 8)
{
//your function here ;
}
?>
PHP是服务器端的,因此您可以调用
echo random_Password();
只在页面加载时发生,并且只发生一次。在javascript中创建函数,并为按钮设置onClick属性。或者像其他人在这些评论中提到的那样使用Ajax。PHP是服务器端的,因此您可以调用
echo random_Password();
只在页面加载时发生,并且只发生一次。在javascript中创建函数,并为按钮设置onClick属性。或者使用其他人在这些评论中提到的Ajax。您遇到的问题是以下代码只运行一次:
var elem = document.getElementById("ww");
elem.value = "<? echo random_Password(); ?>";
var elem=document.getElementById(“ww”);
elem.value=“”;
因此,假设您的random_Password()
函数(完全不可信)返回值12345678,该值将一直保留,直到手动刷新页面。在没有刷新的情况下,每次单击GWW按钮将WW字段设置为12345678
这里有几个不同的选项(最简单到最难):
random_Password()
函数移动到它自己的文件random_Password.php
中,并将“页面”(即8个字符的字符串)加载到文本字段的val
中希望有帮助:)您遇到的问题是以下代码只运行一次:
var elem = document.getElementById("ww");
elem.value = "<? echo random_Password(); ?>";
var elem=document.getElementById(“ww”);
elem.value=“”;
因此,假设您的random_Password()
函数(完全不可信)返回值12345678,该值将一直保留,直到手动刷新页面。在没有刷新的情况下,每次单击GWW按钮将WW字段设置为12345678
这里有几个不同的选项(最简单到最难):
random_Password()
函数移动到它自己的文件random_Password.php
中,并将“页面”(即8个字符的字符串)加载到文本字段的val
中希望这有帮助:)这应该可以正常工作
<td><input type="button" value="Generate" name="gww" onClick="generatePassword()"></td>
<td><input type="text" id="ww"></td>
<script type="text/javascript">
function generatePassword()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for 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","gpass.php",true);
xmlhttp.send();
}
</script>
函数generatePassword()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.document.getElementById(“ww”).value=xmlhttp.responseText;
}
}
open(“GET”,“gpass.php”,true);
xmlhttp.send();
}
我认为您已经有gpass.php为:
<?php
echo random_Password();
function random_Password($length = 8)
{
//your function here ;
}
?>
这应该按原样工作
<td><input type="button" value="Generate" name="gww" onClick="generatePassword()"></td>
<td><input type="text" id="ww"></td>
<script type="text/javascript">
function generatePassword()
{
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{
// code for 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","gpass.php",true);
xmlhttp.send();
}
</script>
函数generatePassword()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{
//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{
//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.document.getElementById(“ww”).value=xmlhttp.responseText;
}
}
open(“GET”,“gpass.php”,true);
xmlhttp.send();
}
我认为您已经将gpass.php作为:
<?php
echo random_Password();
function random_Password($length = 8)
{
//your function here ;
}
?>
然后去做你的问题是什么你不能像这样混合使用PHP和HTML/Javascript。PHP在服务器端、HTML/Javascript客户端运行。要在HTML/Javascript中获得PHP生成的密码,您需要像AJAX这样的技术。它不起作用。它当前总是显示密码,而不是在我单击按钮时显示。那么,你的问题是什么?你不能像这样混合使用PHP和HTML/Javascript。PHP在服务器端、HTML/Javascript客户端运行。在HTML/Javascript中获取PHP生成的密码