从php函数将php变量插入到HTML表单字段中



我编写了一个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";

果不其然,短语"测试1 2 3";插入到2个密码字段中!

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

谢谢!

请删除if条件,因为您没有将任何内容作为查询参数发送,您只调用filename-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;
?>

现在它可以工作了

使用以下代码:这可以使用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;
}
?>

您也可以使用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>

最新更新