Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
单击按钮时在输入字段中显示文本(php/html)_Php_Javascript_Html_Input - Fatal编程技术网

单击按钮时在输入字段中显示文本(php/html)

单击按钮时在输入字段中显示文本(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()

我需要以下方面的帮助;我想在另一个输入字段中单击按钮“gww”时显示新生成的密码(php中的函数)。有人能解释一下吗

<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

这里有几个不同的选项(最简单到最难):

  • 单击时,让GWW按钮执行整页刷新不可取

  • 将random_Password()函数转换为JavaScript代码并在客户端执行。非常简单,但它向黑客揭示了密码是如何生成的,因此并不安全

  • 使用AJAX。将
    random_Password()
    函数移动到它自己的文件
    random_Password.php
    中,并将“页面”(即8个字符的字符串)加载到文本字段的
    val

  • 99%的情况下,我会选择选项3。此处有教程:


    希望有帮助:)

    您遇到的问题是以下代码只运行一次:

    var elem = document.getElementById("ww");
    elem.value = "<? echo random_Password(); ?>";
    
    var elem=document.getElementById(“ww”);
    elem.value=“”;
    
    因此,假设您的
    random_Password()
    函数(完全不可信)返回值12345678,该值将一直保留,直到手动刷新页面。在没有刷新的情况下,每次单击GWW按钮将WW字段设置为12345678

    这里有几个不同的选项(最简单到最难):

  • 单击时,让GWW按钮执行整页刷新不可取

  • 将random_Password()函数转换为JavaScript代码并在客户端执行。非常简单,但它向黑客揭示了密码是如何生成的,因此并不安全

  • 使用AJAX。将
    random_Password()
    函数移动到它自己的文件
    random_Password.php
    中,并将“页面”(即8个字符的字符串)加载到文本字段的
    val

  • 99%的情况下,我会选择选项3。此处有教程:


    希望这有帮助:)

    这应该可以正常工作

    <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生成的密码