【发布时间】:2020-08-22 02:44:17
【问题描述】:
目前,我在网页上有一组员工照片。
每个员工都有一张进出照片。它们分别被命名为firstname_here.jpg 和firstname_away.jpg。
当我点击一张照片时,它会在两者之间切换。
这只是我们在接待处的纵向触摸屏上放置的一个简单的进出板。当员工进来时,他们点击他们的照片以显示 firstname_here.jpg 图像,当他们离开时再次触摸照片将变为 firstname_away.jpg。
我使用了这个例子 https://www.golangprograms.com/javascript-example-to-change-image-on-click.html
并制作了这个工作版本
<!DOCTYPE HTML>
<html>
<script>
function toggleImage(firstname) {
var img1 = "http://localhost:8888/attendance/" + firstname + "_here.jpg";
var img2 = "http://localhost:8888/attendance/" + firstname + "_away.jpg";
var imgElement = document.getElementById(firstname);
imgElement.src = (imgElement.src === img1)? img2 : img1;
var checkstatus = imgElement.src;
if (checkstatus.includes("away")) {
var status = "check_out";
} else {
var status = "check_in";
}
}
</script>
<img src="http://localhost:8888/attendance/david_away.jpg" id="david" onclick="toggleImage('david');"/>
<img src="http://localhost:8888/attendance/ada_away.jpg" id="ada" onclick="toggleImage('ada');"/>
</body>
</html>
现在,我想连接到本地 mysql 数据库,这样当员工点击他们的图片时 它写入名字、状态、当前日期和时间。
我使用这篇文章来设置我的数据库 Design to represent employee check-in and check-out
我与数据库的连接正常,我可以使用手动发送信息
$sql = "INSERT INTO Main (firstname, status)
VALUES ('John', 'check_in')";
我确实为自己的目的更改了我的字段,并且日期和时间是自动生成的。
帮助 我知道 PHP 是基于服务器的,首先运行,而 javascript 是客户端。 我已经浏览了这么多 stackoverflow 页面,并且很难弄清楚当有人单击图像时如何写入 mysql 数据库。
我想写名字和状态变量。
【问题讨论】:
-
只是好奇,这两张照片有什么不同?
-
您不应该需要链接中的
http://localhost:8888部分。比如/attendance/david_away.jpg就足够了
标签: javascript php html mysql ajax