【问题标题】:Timer recorded in phpphp中记录的定时器
【发布时间】:2018-04-07 06:45:21
【问题描述】:

我想为用户创建一个注册表单(姓名、密码、重新输入密码、电子邮件),然后将它们插入 MySQL,我这样做了,但我不知道如何记录用户输入密码所花费的时间.

我正在使用 PHP 和 HTML

【问题讨论】:

  • 你可能对 javascript 中的 onfocus 和 keypress 键盘事件感兴趣

标签: javascript php html


【解决方案1】:

您可以使用setInterval 每秒增加一些数字,在输入获得焦点时开始间隔,并在失去焦点时清除间隔。

    var timeSpent = 0;
    var interval;
    document.getElementById('password2').addEventListener('focus', function() {
        interval = setInterval(function() { 
            timeSpent++;
            document.getElementById('timeSpent').value = timeSpent;
        }, 1000);
    });
    document.getElementById('password2').addEventListener('input', function(event) {
        if (interval && event.target.value === document.getElementById('password').value) {
            clearInterval(interval);
        }
    });
    document.getElementById('password2').addEventListener('blur', function() {
        if (interval) {
            clearInterval(interval);
        }
    });
Password: <input id="password" type="password"/><br/>
Reenter Password: <input id="password2" type="password"/><br/>
Time Spend: <input id="timeSpent" type="number"/><br/>

然后在 PHP 中你会得到类似的东西:

$link = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbusername, $dbpassword);

$statement = $link->prepare("INSERT INTO users(name, password_hash, time_spent)
VALUES(:name, :hash, :time)");

$statement->execute(array(
    "name" => $_POST['username'],
    "hash" => password_hash($_POST['password'], PASSWORD_DEFAULT),
    "time" => $_POST['timeSpent']
));

【讨论】:

  • 如何在我的数据库中存储时间花费?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-08
  • 1970-01-01
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 2013-03-11
  • 1970-01-01
相关资源
最近更新 更多