gpt4 book ai didi

php - 输入类型密码值?

转载 作者:行者123 更新时间:2023-11-30 10:39:48 25 4
gpt4 key购买 nike

你好,我遇到了以下问题:

我有两个输入框。一个是登录用户名,即另一个是密码。现在我想设置两个字段的值。我的问题是密码。我打算实现以下内容:

HTML:

<input 
type="text"
id="log_password"
name="log_password"
value="<?php echo ($log_password);?>"
onfocus="if(this.value=='Password')this.value='';this.type='password'"
onblur="if(this.value=='') this.value=this.defaultValue;"/>

PHP:

$log_password = "Password";

if(isset($_POST['submit'])){

$log_password = $_POST['log_password'];

if(empty($log_password)){
$errors['log_password'][]="No password given";
$_POST['log_password'] = "Password";
$log_password = $_POST['log_password'];
}
}

问题是这会一直工作到提交表单的地步。如果出现错误,由于 type="text" 标记,密码再次可见。那么我该如何解决这个问题,如果有人可以帮助我,我将不胜感激。非常感谢。

更新:

<div class="small2"> 
<?php if (!isset($errors['log_password'])):?>
<input
type="<?= $passwordVal == "" ? "text" : "password" ?>"
id="log_password" name="log_password"
value="<?php echo ($log_password);?>"
placeholder="Passwort"
onfocus="if(this.value=='Passwort')this.value=''"
onblur="if(this.value=='') this.value=this.defaultValue;"/>
<?php endif;?>

<?php if (isset($errors['log_password'])):?>
<input class="log-err"
type="<?= $passwordVal == "" ? "text" : "password" ?>"
id="log_password" name="log_password"
value="<?php echo ($log_password);?>"
placeholder="Passwort"
onfocus="if(this.value=='Passwort')this.value=''"
onblur="if(this.value=='') this.value=this.defaultValue;"/>
<?php endif;?>
</div>

最佳答案

正如 Matt 正确建议的那样,您可以使用 placeholder 标记。

但是要让占位符起作用,您不能设置“值”。当然,解决方案是使用条件类型,或者使用 jQuery 框架。你可以做这样的事情..

<?
$your_pass = 'bla-bla-bla';
?>
<!DOCTYPE html>
<html>
<head>
<title>Test page</title>
<!-- Load jQuery library from Google repository -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<form action="#">
<input type="password" name="log_password" id="log_password" />
</form>
</body>
<script>

$(document).ready(function() {
/* execute this by jQuery when document is ready */

/* must use traditional javascript to change input's type attribute */
document.getElementById('log_password').setAttribute('type', 'text');

/* set empty value attribute of input with id "log_password" */
$('input#log_password').attr('val','');

/* set placeholder attribute */
$('input#log_password').attr('placeholder','Password');

$('input#log_password').focus(function(){
/* when input with id log_password is focus.. */
$(this).attr('placeholder','');

/* set the value as you prefer... */
$(this).val('<?=$your_pass?>');

/* reset type of input to password */
document.getElementById('log_password').setAttribute('type', 'password');
});
});
</script>
</html>

关于php - 输入类型密码值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11727686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com