简体   繁体   English

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

[英]Displaying text in input field when a button is clicked(php/html)

I need some help with the following; 我需要一些帮助以下内容; I want to display a newly generated password(function in php)when the button "gww" is clicked inside another input field. 当在另一个输入字段内单击按钮“gww”时,我想显示一个新生成的密码(php中的函数)。 Could someone shed some 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++;
    }
}
return $password;
}

I suspect you want a new password to be shown whenever you click the button. 我怀疑你想要点击按钮时显示一个新密码。 You have to use AJAX for this. 你必须使用AJAX。

So you have your javascript function: 所以你有你的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();
}

Your HTML should be 你的HTML应该是

<input type="button" value="Generate password" onClick="generatePassword()">

generatePassword.php: generatePassword.php:

<?php
echo random_Password();

function random_Password($length = 8)
{
  //your function here ;
}
?>

This should work as is. 这应该按原样运作。

<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>

I take it you already have gpass.php as: 我认为你已经有了gpass.php:

<?php
echo random_Password();

function random_Password($length = 8)
{
  //your function here ;
}
?>

PHP is serverside, so your call to PHP是服务器端,所以你的电话

echo random_Password();

only happens on page load, and it happens once. 仅在页面加载时发生,并且它发生一次。 Create the function in javascript and have an onClick attribute for the button. 在javascript中创建函数并具有按钮的onClick属性。 Or use Ajax as mentioned by someone else in these comments. 或者在这些评论中使用其他人提到的Ajax。

The problem you're having is that the following code only runs once: 您遇到的问题是以下代码只运行一次:

var elem = document.getElementById("ww");
elem.value = "<? echo random_Password(); ?>";

So, suppose your random_Password() function (totally implausibly) returned the value 12345678 , this value is held until the page is manually refreshed. 因此,假设您的random_Password()函数(完全令人难以置信)返回值12345678 ,此值将保持到手动刷新页面为止。 With out a refresh, clicking the GWW button will set the WW field to 12345678 every time. 无需刷新,单击GWW按钮将每次将WW字段设置为12345678

You have a few different options here (easiest to hardest): 你有几个不同的选择(最简单到最难):

  1. Have the GWW button perform a full-page refresh when clicked. 单击时,让GWW按钮执行整页刷新。 Not desirable. 不可取的。

  2. Convert your random_Password() function into JavaScript code and execute it in the client. 将random_Password()函数转换为JavaScript代码并在客户端中执行。 Very straightforward, but it reveals to hackers how passwords are being generated, and as such is not secure . 非常简单,但它向黑客揭示了如何生成密码,因此不安全

  3. Use AJAX . 使用AJAX Move your random_Password() function into it's own file, random_password.php and load the "page" (ie an 8-character string) into the text field's val . random_Password()函数移动到它自己的文件random_password.php ,并将“page”(即8个字符的字符串)加载到文本字段的val

I would opt for Option 3 in 99% of cases. 我会在99%的情况下选择备选方案3 Tutorial here: http://www.w3schools.com/php/php_ajax_php.asp 这里的教程: http//www.w3schools.com/php/php_ajax_php.asp

Hope that helps :) 希望有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM