drhwb470572 2014-09-29 08:05
浏览 22
已采纳

PHP中的日历下拉:保持选定的值并重新使用该功能

I have found online the following PHP function on defining a calendar drop-down (in month/day/year format). The function is working fine, but I would like:

a) Keep the selected value for Month, Day and Year, so to be able to edit values easily (values are defined dynamically through a foreach loop), and

b) would like to re-use the function on multiple variables (so far, I am using it to define date of birth, but I would like to use the same function also on additional fields too).

The fact that the values for Month, Day and Year are defined dynamically is a little bit confusing me, and of course I am not fluent in functions definition yet. Any help would be greatly appreciated !

Here is the function:

function make_calendar_pulldowns() {

    // Make the months array:
    $months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

    // Make the months pull-down menu:
    //echo '<p><label for="dob" style="font-family: Verdana, Arial; font-size: 1.0em; font-weight: 600; color: #595959; line-height: 1.9em;">Date of Birth</label></p>';
    echo '<select required="required" name="month" id="month" style="width: 33%; display: inline; float: left; margin-left: 1%; margin-right: 0%">';
    echo '<option selected value="">Month</option>
';
    foreach ($months as $key => $value) {
        echo "<option value=\"$key\" >$value</option>
";
    }
    echo '</select>';

    // Make the days pull-down menu:
    echo '<select required="required" name="day" id="day" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
    echo '<option selected value="">Day</option>
';
    for ($day = 1; $day <= 31; $day++) {
        echo "<option value=\"$day\">$day</option>
";
    }
    echo '</select>';

    // Make the years pull-down menu:
    echo '<select required="required" name="year" id="year" style="width: 33%; display: inline; float: left; margin-left: 0%; margin-right: 0%" ;>';
    echo '<option selected value="">Year</option>
';
    for ($year = 1910; $year <= 2000; $year++) {
        echo "<option value=\"$year\">$year</option>
";
    }
    echo '</select>';

Once values have been defined, I convert them to MySQL format with the following code:

// Validate the month.
if (is_numeric ($month)) {
    $dob = $month . '-';

} else {
    $action['result'] = 'error'; 
    array_push($text,'Please insert a valid Month for patient birth date');
}
// Validate the day.
if (is_numeric ($day)) {
    $dob .= $day . '-';

} else {
    $action['result'] = 'error'; 
    array_push($text,'Please insert a valid Day for patient birth date');
}
// Validate the year.
if (is_numeric ($year)) {
    $dob = $year . '-' . $month . '-' .  $day; // Set Birthdate in SQL format

Finally, I call the function in the form with the following code:

<!-- <p><label for="dob">Date of Birth <img src="../img/req.gif" alt="Required"</label></p>
<?php make_calendar_pulldowns(); ?> -->
  • 写回答

1条回答 默认 最新

  • duanpuchen3142 2014-09-29 08:22
    关注

    Actually some parts of it are a bit off:

    // Make the months array:
    $months = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    // some months are a bit off, the keys are missing
    // Should be something like this:
    
    $months = array ('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    $month_num = range(1, 12);
    $months = array_combine($month_num, $months);
    

    And the days are hardcoded, what about February 31?

    The Years I guess should be okay.

    Why not use a date picker (jQuery UI in particular) for this purpose instead?

    $(function() {
        $( "#datepicker" ).datepicker({
            changeMonth: true,
            changeYear: true,
            dateFormat: 'yy-mm-dd', // iso format
        });
    });
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
    
    <form method="POST">
      <p>Date: <input type="text" id="datepicker" name="date"></p>
      <input type="submit" name="submit1" />
    </form>

    Then in PHP, just call it like a normal $_POST

    if(isset($_POST['submit1'])) {
        $date = $_POST['date'];
    }
    

    More API Info: http://jqueryui.com/datepicker/

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本