【问题标题】:insert php variable into HTML form field from php function从 php 函数将 php 变量插入 HTML 表单字段
【发布时间】:2021-09-26 19:11:55
【问题描述】:

我编写了一个 php 函数,它根据一组规则生成随机密码。在用户单击调用 php 函数的“生成随机密码”按钮(不刷新页面)后,我想将 $randomPassword 变量插入 HTML 表单字段。一旦随机密码出现在表单字段中,用户单击“提交”按钮以更改数据库中的密码。

将 php 变量放入表单字段的最佳方法是什么?我已经进行了大量搜索,但无法提出可行的解决方案。

任何有关如何实现此目的的提示将不胜感激。谢谢!

*** 编辑 ***

感谢以下所有反馈,但我今天找到了工作的起点,这是表单:

                <form name="passwordform" action="" method="post">
                    <div>
                        <?php
                            if ( $error )
                                echo '<font color="red">'. $message . "<br></font>";
                           else
                               echo $message;
                        ?><br>
                    </div>
                    <div class="wrap-input100 validate-input" data-validate = "password is required">
                        <div class="content">
                            <input class="input100" type="password" id="pass1" name="new_password" placeholder="New Password"</input>
                            <span class="focus-input100-1"></span>
                            <span class="focus-input100-2"></span>
                        </div>
                    </div><br>
                    <div class="wrap-input100 validate-input" data-validate = "password is required">
                        <div class="content">
                            <input class="input100" type="password" id="pass2" name="confirm_new_password" placeholder="Confirm New Password"</input>
                            <span class="focus-input100-1"></span>
                            <span class="focus-input100-2"></span>
                        </div>
                    </div><br>
                    <div class="first">
                        <div class="group1">
                            <input type="checkbox" id="chk">
                            <label id="showhide" class="label">Show Passwords</label>
                        </div>
                    </div>
                    <script src="_js/showhide.js"></script><br>
                    <?php echo $passwordSyntaxMessage . "<br>";
                        if ($reCAPTCHAcheck)
                            echo '<div class="g-recaptcha" data-sitekey=' . $reCAPTCHAsiteKey . '></div><br>';
                    ?>
                    <input class="login100-form-btn" name="insert" type="button" value="Generate Random Password" onClick="randomPassword();" /><br>
                    <div style="float:left; padding-right: 0px">
                        <input class="form-button" style="width: 180px" name="insert" type="submit" value="Set Password" />
                    </div>
                    <div style="float:right; padding-right: 0px">
                        <input class="form-button" style="width: 180px" name="insert" type="reset" value="Reset Form" />
                    </div>
                </form>
                <script type="text/javascript" >
                    function randomPassword() {
                        var pass = <?php echo generateRandomPassword();?>; 
                        passwordform.new_password.value = pass;
                        passwordform.confirm_new_password.value = pass;
                    }
                </script>

如果我单击“生成随机密码”按钮,则 2 个密码字段保持空白。 php 函数 generateRandomPassword() 在一个名为 functions.php 的文件中,我知道它可以工作,因为我可以在浏览器中回显结果。

如果我换行:

var pass = <?php echo generateRandomPassword();?>;

与:

var pass = "testing 1 2 3";

果然在2个密码字段中插入了“testing 1 2 3”这句话!

那么调用 php 函数我哪里出错了?

谢谢!

【问题讨论】:

    标签: php forms insert passwords field


    【解决方案1】:

    请删除 if 条件,因为您没有发送任何查询参数,您只是调用文件名 action.php

                <?php
    
                    // Add codes your desired PHP function that generate password. I am adding some code that i got from somewhere.
                    
                    $alpha = "abcdefghijklmnopqrstuvwxyz";
                    $alpha_upper = strtoupper($alpha);
                    $numeric = "0123456789";
                    $special = "!@$#*%";
    
                    // Concatinate all variables into one long string
                    $chars = $alpha . $alpha_upper . $numeric . $special;
    
                    // Select password length
                    $length = 10;
    
                    // Suffle the value of $chars
                    $chars = str_shuffle($chars);
    
                    // Return the length of your new $chars string
                    $len = strlen($chars);
    
                    // Create empty variable that will hold your new password
                    $pw = '';
    
                    // A simple 'for' statement that will select random characters for the lenth of your password
                    for ($i=0;$i<$length;$i++)
                        $pw .= substr($chars, rand(0, $len-1), 1); 
    
                    // Store the finished password in a variable, that will shuffle the value created by the 'for' statement
                    $pw = str_shuffle($pw);
    
                    // show the password on screen
                    echo $pw;
    
                ?>
    

    现在它可以工作了。

    【讨论】:

    • 我根据你的建议让它工作了。我将我的函数从functions.php中生成密码放到它自己的php文件中,它现在可以正常工作了。感谢您的提示!
    【解决方案2】:

    使用以下代码:这可以使用 AJAX 来完成。

    index.html

    <form action="" method="">
        <label>Password:</label>
        <input type="text" name="password" id="password" />
        <button type="button" onclick="generate_password();">Generate</button>
        <br /><br />
        <input type="submit" />
    </form>
    <script>
    function generate_password()
    {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function()
        {
            if (this.readyState == 4 && this.status == 200)
            {
                document.getElementById("password").value = this.responseText;
            }
        };
        xmlhttp.open("GET", "action.php", true);
        xmlhttp.send();
    }
    </script>
    

    action.php

    <?php
    if (isset($_GET['generate_password']))
    {
        // Add codes your desired PHP function that generate password. I am adding some code that i got from somewhere.
        
        $alpha = "abcdefghijklmnopqrstuvwxyz";
        $alpha_upper = strtoupper($alpha);
        $numeric = "0123456789";
        $special = "!@$#*%";
    
        // Concatinate all variables into one long string
        $chars = $alpha . $alpha_upper . $numeric . $special;
    
        // Select password length
        $length = 10;
    
        // Suffle the value of $chars
        $chars = str_shuffle($chars);
    
        // Return the length of your new $chars string
        $len = strlen($chars);
    
        // Create empty variable that will hold your new password
        $pw = '';
    
        // A simple 'for' statement that will select random characters for the lenth of your password
        for ($i=0;$i<$length;$i++)
            $pw .= substr($chars, rand(0, $len-1), 1); 
    
        // Store the finished password in a variable, that will shuffle the value created by the 'for' statement
        $pw = str_shuffle($pw);
    
        // show the password on screen
        echo $pw;
    }
    ?>
    

    【讨论】:

      【解决方案3】:

      你也可以使用 JQuery AJAX

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
      

      JQuery

      <script>
         function generate_password(){
          $.ajax({
                  method: "get",
                  data: {generate_password: true},
                  url: "action.php",
                  success: function(data){
                      $("#password").val(data)
                  }
              });
          }
      </script>
      

      【讨论】:

        猜你喜欢
        • 2019-05-21
        • 2014-05-15
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 2015-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多