【问题标题】:Write time and date on checkbox change into a textfile将复选框更改的时间和日期写入文本文件
【发布时间】:2018-01-29 05:20:36
【问题描述】:

我无法获取复选框更改的时间和日期。

我有一个名为“面板”的 MySQL 数据库,其中填充了用户条目,每个条目在数据库中都有一个“状态”字段,即“1”或“0”。 我正在使用此状态来查看谁在线并将复选框设置为按钮,1 = 绿色,0 = 灰色。

现在我想在此状态更改时获取当前系统时间并将其写入文本文件。

例如: 当状态从 0 变为 1 时,将以下文本写入文档: [当前时间] + [MySQL 数据库中的用户名] + “登录”

当状态从 1 变为 0 时,写入: [当前时间] + [mySQL 数据库中的用户名] + “注销”

这是我的代码:

#this is where I change the status field in MySQL

<?php
$id = $_POST["id"];
$update = mysql_query("update panel 
    SET status = CASE
        WHEN status = '1' THEN '0'
        WHEN status = '0' THEN '1'          
    END 
WHERE id = $id")
?>

#this is where I execute the function

<td>
<script type="text/javascript" src="scripte/jquery-2.1.4.min.js"></script> 
<script>
function save_checkbox(id)
{
$.post( 'save_check.php' , { checked : $(this).attr("checked"), id: id });
}
</script>
<div class="switch anws">
<input type="checkbox" name="anw_status" value="1" onchange="save_checkbox(<?php echo "$row->id"; ?>);" <?php if ($row->status==1) echo "checked";?>>
<label class="label"><p><?php echo "$row->gender $row->person";?></p></label>
</div>
</td>

【问题讨论】:

标签: javascript php jquery mysql datetime


【解决方案1】:

我建议为此在您的数据库中创建一个单独的表,而不是将其写入文本文件。

但是,您可以通过以下方式写入 txt 文件: 假设您在与save_check.php 相同的文件夹中创建了一个名为log.txt 的文本文件,那么当调用save_check.php 时,您可以这样做:

首先添加一个sql查询来获取新的状态和用户名并将它们设置为变量$state$name然后

$my_file = 'log.txt';
$handle = fopen($my_file, 'a') or die('Cannot open file:  '.$my_file);

if ($state == 0 {
  $data = "\n" . date('m/d/Y h:i:s a', time()) . " " . $name . " logged out";
} else {
  $data = "\n" . date('m/d/Y h:i:s a', time()) . " " . $name . " logged in";
}

fwrite($handle, $data);
fclose($handle);
  • \n 是为了确保它在一个新的行上。
  • date('m/d/Y h:i:s a', time()) 将获取当前时间并格式化
  • fwrite 将附加到文件中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多