1

您好,我对以下问题有疑问:

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

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>
4

5 回答 5

2

采用

<input type="password" placeholder="Password" ... />

如果您必须将其用作文本字段(为了显示“密码”),您应该使输入类型有条件:

<?php
    // in case of error, the value that the user entered will be passed back via GET (POST?)
    $passwordVal = isset($_GET['log_password']) ? $_GET['log_password'] : "";
?>

<!-- if ($passwordVal) is an empty string, type is "text", else type is "password". -->
<input type="<?= $passwordVal == "" ? "text" : "password" ?>" ... />
于 2012-07-30T18:48:42.750 回答
2

正如马特正确建议的那样,您可以使用占位符标签。

但是要让占位符起作用,您不能设置“值”。解决方案当然是使用条件类型,或者使用 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>
于 2012-07-30T19:21:55.080 回答
0

我认为,如果有错误,您需要将类型从“文本”更改为“密码”

于 2012-07-30T18:51:12.737 回答
0

编写你认为是函数的代码,然后通过加载页面、焦点事件和模糊事件调用它。Matt 的解决方案很好,但我认为它仅限于支持 HTML5 的浏览器。

于 2012-07-30T18:58:03.190 回答
0

文档上的脚本如何准备检查 log_password 值是否为密码并更改类型。

哦,抱歉,请参阅 deste 的帖子 +1

于 2012-07-30T19:53:04.023 回答