简体   繁体   English

为什么我不能让HTML表单将值传递给PHP?

[英]Why can't I get HTML form to pass values to PHP?

This is pretty basic, but I'm new to web development. 这是非常基础的,但我是Web开发的新手。 I have a form in HTML that takes time and passed it to a variable in PHP script. 我有一个HTML格式的表单需要花费时间并将其传递给PHP脚本中的变量。 That works fine. 这很好。 However, I try to now take the date from the user in HTML, and pass it the same way to a variable in PHP and I get nothing (print shows that the variable is empty). 但是,我现在尝试以HTML格式从用户那里获取日期,并以相同的方式将其传递给PHP中的变量,我什么也得不到(print显示变量为空)。

Here is my code: 这是我的代码:

<form action="" method="post">
    <select name="time">
        <!-- some values here -->
    </select>
    <input type="text" id="datepicker">
    <input type="submit">
</form>


<?php 
    $search = $_POST['time'];
    // it prints data
    print($_POST['time']);

    // it prints none
    $search2 = $_POST['datepicker'];
    print($search2);
?>

The first print ($search) does print out the time selected by the user. 第一次打印($ search)确实打印出用户选择的时间。 However, the second print ($search2) does not print out the date selected. 但是,第二次打印($ search2)不会打印出所选日期。 Where did I go wrong? 我哪里做错了?

Add name to your datepicker: 为您的日期选择器添加名称:

<input type="text" id="datepicker" name="datepicker">

Posted values are accessable via input_name keys, not ids or classes. 发布的值可通过input_name键访问,而不是ID或类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM