[英]insert php variable into HTML form field from php function
我编写了一个 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 函数时哪里出错了?
谢谢!
使用以下代码:这可以使用 AJAX 来完成。
索引.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>
动作.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;
}
?>
请删除 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;
?>
现在它将起作用。
您也可以使用 JQuery AJAX
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
查询
<script>
function generate_password(){
$.ajax({
method: "get",
data: {generate_password: true},
url: "action.php",
success: function(data){
$("#password").val(data)
}
});
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.