【问题标题】:Age calculator does not show computed age年龄计算器不显示计算的年龄
【发布时间】:2017-04-26 09:24:10
【问题描述】:

此代码用于根据使用组合框的用户的选定值计算年龄,它必须在下面提供的文本框中显示年龄。每次用户更改组合框中的选定值时,必须刷新年龄。但我当前的代码没有显示计算的年龄。

<?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>

每次更改组合框的值时,文本框都会显示 0。 让我们假设文本框的值是 1991 年 10 月 19 日,它应该显示 21

【问题讨论】:

  • 请详细描述您的问题。告诉我们到底发生了什么,任何错误消息和how you have tried to fix the problem.
  • onSelect/onChange 监听器在哪里?
  • 我编辑了我在每个 中放置的代码
  • 如果问题不在于 PHP,那么请仅显示呈现的 HTML/Javascript

标签: php javascript html


【解决方案1】:

将 javascript 向上移动并使其成为一个函数,以便您可以引用它

然后添加 onChange() 以在您更改选择元素时触发该功能

终于让函数将你的计算输出到页面上的某个地方

<?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>

【讨论】:

  • 每次我更改组合框的值时,它都会返回 0。:(
  • 看起来你的一些变量计算出了问题..让我把它换掉并重新发布我的答案
  • 答案已更新为从选择字段中提取当前值,而不是使用 javacsript 中的 php 值。看看有没有帮助。
  • 嗯。代码返回 NaN 值,对于 echo $Convertdays,它会回显当前日期。
猜你喜欢
  • 2012-03-26
  • 1970-01-01
  • 1970-01-01
  • 2020-12-28
  • 2016-03-20
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多