将html 选项设置为基于当前时间的默认值(setting html option to be the default value based on current time)

编程入门 行业动态 更新时间:2024-10-27 18:20:43
将html option to be the default value based on current time)

我已经制作了一个基本的打卡网页和MySQL数据库,唯一的问题是当人们意外地在一天结束而不是在一天结束时,或反之亦然,它留下了很多空白。

我需要某种代码,在10:30之前将“startfinish”值默认为“Start”,在14:30之后默认为“End”但仍然允许用户在需要时更改选项

注意确实从哪里开始,如果我应该使用javascript或php(php在单独的文件中作为“表单操作”)

继承了我当前需要更改选项的html代码:

<select name="startfinish" id="startend" required> <option selected="selected" disabled selection>Please select</option> <option value="Start">Start</option> <option value="End">End</option>

任何帮助将不胜感激

谢谢,

丹尼尔

I have made a basic punchclock webpage & MySQL database, only problem is when people accidently clock in instead of out at end of day or vice versa it leaves alot of gaps.

I need some sort of code that defaults "startfinish" value to "Start" before 10:30 and "End" after 14:30 but still allows the user to change the option if they needed to

note really sure where to start and if i should use javascript or php (php is in a seperate file as the "form action")

heres my current html code that needs the option changed:

<select name="startfinish" id="startend" required> <option selected="selected" disabled selection>Please select</option> <option value="Start">Start</option> <option value="End">End</option>

any help would be greatly appreciated

thanks,

Danial

最满意答案

如果您不反对PHP,可以检查当前时间并将其与您指定的时间进行比较。 这必须包含在您正在处理的文件中,而不是您的操作文件。

<?php // Set the default timezone so you don't run into timezone issues // You can set this to whatever suits your application best // Full list of supported timezones here: http://php.net/manual/en/timezones.php date_default_timezone_set('America/Vancouver'); // Compare the current time to the start and end times // This reads: if current time is before start time, option is selected, otherwise it's not $start = (strtotime('now') < strtotime('10:30 AM today') ? 'selected="selected"' : ''); // This reads: if current time is after end time, option is selected, otherwise it's not $end = (strtotime('now') > strtotime(' 2:30 PM today') ? 'selected="selected"' : ''); ?>

要在您的选择控件中使用它,您可以在选项中回显变量(为简洁起见,以简写形式显示)。 如果日期计算正确,将选择该选项,否则它将保持不变。 我从占位符中删除了选择,因为此设置将确保始终选中“开始”或“结束”。

<select name="startfinish" id="startend" required> <option disabled selection>Please select</option> <option <?=$start?> value="Start">Start</option> <option <?=$end?> value="End">End</option> </select>

在这种情况下使用PHP的好处是它运行服务器端而不是客户端,因此您不必担心用户禁用Javascript并破坏您的表单设计。 但是,如果您已经在应用程序中依赖jQuery,则可能不是问题。

If you aren't opposed to PHP you could check the current time and compare it against the times you've specified. This will have to go in the file you're working from though, not your action file.

<?php // Set the default timezone so you don't run into timezone issues // You can set this to whatever suits your application best // Full list of supported timezones here: http://php.net/manual/en/timezones.php date_default_timezone_set('America/Vancouver'); // Compare the current time to the start and end times // This reads: if current time is before start time, option is selected, otherwise it's not $start = (strtotime('now') < strtotime('10:30 AM today') ? 'selected="selected"' : ''); // This reads: if current time is after end time, option is selected, otherwise it's not $end = (strtotime('now') > strtotime(' 2:30 PM today') ? 'selected="selected"' : ''); ?>

To use this in your select control, you'd echo the variables (shown in shorthand for brevity) in the options. If the date calculations are correct, that option will be selected, otherwise it will remain unchanged. I removed the selection from the placeholder because this setup will ensure Start or End are always selected.

<select name="startfinish" id="startend" required> <option disabled selection>Please select</option> <option <?=$start?> value="Start">Start</option> <option <?=$end?> value="End">End</option> </select>

The benefit of using PHP in this case is that it runs server-side instead of client-side, so you don't have to worry about the user disabling Javascript and ruining your form design. Though, if you're already depending on jQuery in your application that's probably a non-issue.

更多推荐

本文发布于:2023-07-07 15:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1065193.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   默认值   选项   时间   html

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!