douxingti9307 2013-09-29 19:41
浏览 86
已采纳

如何通过onclick按钮为文本框分配值?

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("
", $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>
  • 写回答

2条回答 默认 最新

  • duang8642 2013-09-29 20:04
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作