简体   繁体   English

年龄计算器不显示计算出的年龄

[英]Age calculator does not show computed age

This code is for computation of age based on the Selected Value of the user using the combo box and it must display the age in the text box provided below. 该代码用于使用组合框基于用户的“选定值”计算年龄,并且必须在下面提供的文本框中显示年龄。 The age must refreshed every time the user changes selected value in the combo box. 每次用户更改组合框中的选定值时,年龄都必须刷新。 but my current code does not the computed age does not show. 但我当前的代码未显示计算出的年龄。

<?php
                $month = date("m"); //without leading zero(o)
                $year = date("Y"); //four digit format
                $day = date("d");
                $st_year = "1950"; //Starting Year
                $month_names = array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
            ?>

            <form name="Month_Year" id="Month_Year"  method="post">
            <select name="month" id="month">
            <?php
            for ($i=1; $i<=12; $i++) {
                echo "<option ";
                if ($i == $month) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">", $month_names[$i-1], "</option>\n";
            }
            ?>
            </select>
            <select name="year" id="year">
            <?php
            for ($i=$st_year; $i<=$year; $i++) {
                echo "<option ";
                if ($i == $year) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">$i</option>\n";
            }
            ?>
            </select>
            <select name="day" id="day">
            <?php
            for ($i=1; $i<=31; $i++) {
                echo "<option> ";
                if ($i == $day) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">$i</option>\n";
            }
            ?>
            </select>
            // I used this code to combine the selected value in the combo box.
            <?php $Convertdays = $month."/".$day."/".$year; 

            echo $Convertdays;
            ?>
            <script type="text/javascript">
            var birth = new Date( <?php '$Convertdays'?>);
            var check = new Date();
            var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds;
            var ageInDays = (check - birth) / milliDay;
            var ageInYears =  Math.floor(ageInDays / 365 );
            var age =  ageInDays / 365 ;
            </script>

Every time I change the value of the combo box, the textbox displays 0. Lets assume that the values of the Textboxes are October 19 1991 it should display 21 每次更改组合框的值时,文本框都会显示0。假设文本框的值为1991年10月19日,则应该显示21。

Move the javascript up top and make it a function so you can reference it 将javascript向上移动并使其具有功能,以便您可以引用它

Then add the onChange() to trigger the function when you change your select elements 然后添加onChange()以在您更改选择元素时触发功能

finally have the function output your calculation to somewhere on the page 最后让函数将您的计算结果输出到页面上的某个位置

<?php
    $month = date("m"); //without leading zero(o)
    $year = date("Y"); //four digit format
    $day = date("d");
    $st_year = "1950"; //Starting Year
    $month_names = array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December");
    $Convertdays = $month."/".$day."/".$year; 
?>
<script type="text/javascript">
    function update() {
        var e = document.getElementById("month");
        var month = e.options[e.selectedIndex].value;
        e = document.getElementById("day");
        var day = e.options[e.selectedIndex].value;
        e = document.getElementById("year");
        var year = e.options[e.selectedIndex].value;
        var birthDate = month + "/" + day + "/" + year
        var birth = new Date(year,month,day,0,0,0);
        var check = new Date();
        var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds;
        var ageInDays = (check - birth) / milliDay;
        var ageInYears =  Math.floor(ageInDays / 365 );
        var age = parseInt(ageInDays / 365) ;
        if (age < 3) {
            document.getElementById('Calculated').innerHTML = "You are under 3 years old, why are you on the computer?";
        } else {
            document.getElementById('Calculated').innerHTML = "You are " + age;
        }
    }
</script>
Enter your birth Date:
<div style="height:15px;"></div>
<form name="Month_Year" id="Month_Year"  method="post">
    <select name="month" id="month" style="margin-right: 30px;" onChange="update();">
        <?php
            for ($i=1; $i<=12; $i++) {
                echo "<option ";
                if ($i == $month) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">", $month_names[$i-1], "</option>\n";
            }
        ?>
    </select>
    <select name="day" id="day" style="margin-right: 30px;" onChange="update();">
        <?php
            for ($i=1; $i<=31; $i++) {
                echo "<option ";
                if ($i == $day) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">$i</option>\n";
            }
        ?>
    </select>
    <select name="year" id="year" onChange="update();">
        <?php
            for ($i=$st_year; $i<=$year; $i++) {
                echo "<option ";
                if ($i == $year) {
                    echo "selected=\"selected\" ";
                }
                echo "value=\"$i\">$i</option>\n";
            }
        ?>
    </select>
    <br>
</form>
<div id="Calculated">
    calculated age goes here<br>
</div>

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

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