PHP 表单生成器
如何使用 PHP 生成表单的各个部分?
我想制作一个允许用户输入日期的表单 分别通过月、日、年的下拉菜单,无需逐一输入 菜单选项,我如何使用 PHP 自动执行此操作?
例如,如果不使用纯 HTML 手动输入,如何生成 1 月的 1-31 天、2 月的 1-28 天等?
How would I generate parts of a form using PHP?
I would like to make a form that allowed the user to enter a date
through drop-down menus of month, day, and year respectively without typing every single
option of the menu, how would I automatically do so with PHP?
For example, how would I generate the days 1-31 for January, 1-28 for February, and so onwards without manually entering them using pure HTML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我在 PHP 中执行此操作的简化版本。
如果您要经常使用它们,您可以创建输出它们的函数。
编辑:我不花时间自动填充日期,因为日期仍应在服务器端检查。
Here's a simplified version of how I do it in PHP
You can create functions that output them if you are going to be using them a lot.
Edit: I don't take the time to autopopulate the days because the date should still be checked server side.
我认为 HTML5有一个日期选择器组件,但也许现在使用它还为时过早。
Galen 走在正确的轨道上,但是当您在浏览器中从一个月份更改为另一个月份时,您将无法获得正确的日期设置(即:如果您不回拨到 2 月 31 日,您最终会得到 2 月 31 日)服务器)
Itay Moav 的 Javascript 建议我认为是可行的方法:使用 javascript 在浏览器端修改 DOM。 当用户选择二月时,触发月份选择器的 onChange 事件,您将重写日期选择器,并创建 28-29 天,具体取决于
我所做的年份 (!) 类似的内容一次,您应该能够毫不费力地分解页面/脚本(该网站是日语的,但是源代码写得不好)。
I think HTML5 has got a date selector component, but maybe it's too early for using that.
Galen is on the right track, but as you change from one month to the other in the browser, you won't get the correct date settings (ie: you'll end up with February 31st if you don't call back to the server)
Itay Moav's Javascript suggestion I believe is the way to go: Use javascript to modify the DOM at the browser end. When the user selects February, triggerring an onChange event for the month selector, you would rewrite the date selector, and create 28-29 days, depending on the year (!)
I did something along these lines once, you should be able to pull apart the page/script without too much difficulty (The site's in Japanese, but the source code is poorly written).
两个答案:
1:找到一个 JavaScript Calendar 类。
2:在 PHP 中:
现在您有一个月份数组(映射),按月索引,值设置为该月的天数;
时间代码基本上是这样说的:找到下个月的第一天 - 然后找到昨天(下个月的第一天的前一天) - 然后返回这一天。 所以该月的最后一天就是该月的天数。
-CF
Two Answers:
1: Find a JavaScript Calendar class.
2: In PHP:
Now you have an array (map) of months, index by month, with values set to the number of days in the month;
The time code basically says this: Find the first day of the next month - then find yesterday (the day before the 1st day of the next month) - then return the day. So the last day of the month is the number of days in that month.
-CF
另外,如果您想显示脚本执行当月的天数,您可以这样做:
这将输出 30、31 或 28(如果是二月)
Also, if you want to display the number of days in the month the script is being executed, you can do:
This will output either 30, 31, or 28(if february)