使用 PHP 回显通过 html Select 发送的信息

发布于 2024-10-21 03:06:13 字数 1003 浏览 8 评论 0原文

我有 3 个选择框,一个代表日、月和年。

一旦用户选择了他们想要的日期、月份和年份,他们就会点击“发送”。

当他们单击“发送”时,我希望 PHP 回显他们的选择。

到目前为止我拥有的代码是:

<?php
$day = array(range(1,31));
$month = array(range(1,12));
$year = array(range(2011,2020));
?>
Day:
<select>
  <?php foreach($day[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Month:
<select>
  <?php foreach($month[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Year:
<select>
  <?php foreach($year[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<input type='submit' value='Send' name='poll' />

Date Selected: 

任何建议、改进、链接代码示例将不胜感激。

I have 3 select boxes, one for day, month and year.

Once the user chooses their desired day, month and year they click 'send'.

When they click send I want PHP to echo out their selection.

The code I have so far is:

<?php
$day = array(range(1,31));
$month = array(range(1,12));
$year = array(range(2011,2020));
?>
Day:
<select>
  <?php foreach($day[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Month:
<select>
  <?php foreach($month[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<br>
Year:
<select>
  <?php foreach($year[0]++ as $key => $value) { ?>
    <option value="<?php echo $key ?>"><?php echo $value ?></option>
  <?php }?>
</select>
<input type='submit' value='Send' name='poll' />

Date Selected: 

Any suggestions, improvements , links code examples would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云淡月浅 2024-10-28 03:06:13

您所描述的内容无法单独使用 PHP 来完成。您需要使用客户端脚本来了解用户如何与 DOM 交互。有许多不同的方法可以实现这一点。最好的选择是使用 AJAX 框架,例如 jQuery(如上所述)、PrototypeDojoYUI。我可能会选择 jQuery,因为如果您习惯了 css 选择器,那么它很容易上手。

首先,您需要稍微更改 html,为输入元素提供 ID类名称。并且,还添加一个显示元素,在本例中为 DIV。然后你可以像这样选择那些 DOM 节点:

     <script>
        $( document ).ready( function () {
            $( '#send' ).click( function () {

                var span = document.createElement( 'span' );
                $( span ).attr( 'id', 'options' );

                $( 'select' ).each( function () {
                    $( span ).append($( this ).val() + " ");
                });                 

                $( '#show' ).html( span );

            });
        });
    </script>

我模拟了一个快速小提琴: JSFIDDLE

What you're describing can't be done with PHP alone. You need to use client side scripting to know how the user is interacting with the DOM. There are many different ways that you could accomplish this. Your best bet is to use an AJAX framework like jQuery (as mentioned above), Prototype, Dojo, or YUI. I'd probably go with jQuery, because it's very easy to pick up if you're used to css selectors.

First off you'd change your html a little, providing ID or Class name for the input element. And, also adding a display element, in this case a DIV. Then you could select those DOM nodes like this:

     <script>
        $( document ).ready( function () {
            $( '#send' ).click( function () {

                var span = document.createElement( 'span' );
                $( span ).attr( 'id', 'options' );

                $( 'select' ).each( function () {
                    $( span ).append($( this ).val() + " ");
                });                 

                $( '#show' ).html( span );

            });
        });
    </script>

I mocked up a quick Fiddle this: JSFIDDLE

街道布景 2024-10-28 03:06:13

请改用全面的日期选择器,例如 datepicker 内置的 datepicker。 com/" rel="nofollow">jQuery UI.他们在现场有例子。

如果您沿着 3 个下拉菜单选择路线,您要么需要在那里进行一些时髦的动态切换,要么为无效日期做好准备。 2月31号有人吗? :)

Use a comprehensive datepicker instead, like the datepicker built into jQuery UI. They have examples on-site.

If you go down the 3 dropdown selection route, you'll either need to have some funky dynamic switching in there, or be prepared for invalid dates. 31st of February anyone? :)

ぶ宁プ宁ぶ 2024-10-28 03:06:13

首先,您需要指定标签,并指定数据的去向。其次,您需要命名输入字段以便可以检索它们。

您可能想看看 http://www.w3schools.com/php/php_forms.asp

First off, you need to specify tags, and specify where you want the data to go. Second, you need to name your input fields to you can retrieve them.

You might want to take a look at http://www.w3schools.com/php/php_forms.asp

绿光 2024-10-28 03:06:13

表格邮寄到哪里?尝试回显这些值的代码在哪里?这里是选择元素,其选项是在页面呈现时生成的,但是下一步在哪里呢?

提示:您的 select 元素需要名称,以便随时访问这些值。此外,如果您想要做的就是向用户显示所选的值,则无需将表单发布到任何地方或在服务器端执行此操作。 JavaScript 可以很好地完成这项工作:)

Where does the form post? Where is the code that attempts to echo the values? What you have here is the select elements with their options being generated when the page is rendered, but where's the next step?

Hint: Your select elements will need names in order to access those values at any time. Also, if all you want to do is show the user the values that were selected, you don't need to post the form anywhere or do this on the server-side. JavaScript can do the job just fine :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文