使用php输入更改表值

时间:2015-05-12 09:25:39

标签: php jquery html

我正在使用html表制作日历并使用jquery填充输入。 但它不起作用,表格中没有任何内容填充。只有在按下按钮后才会显示空表。

这是代码,plz指出我错了:

<?php
date_default_timezone_set('Asia/Kolkata');

$inputMonth = date('Y-m-d');

$totalday = date("t", strtotime($inputMonth)); 
$month = date("m" , strtotime($inputMonth));
$year = date("Y" , strtotime($inputMonth));
$getdate = getdate(mktime(null, null, null, $month, 1, $year));
$first_day = $getdate["weekday"];   
$day_of_week = date('N', strtotime($first_day));        
?>

<html>
    <head>
        <title>Calendar Test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $("button").click(function() {
                    var firstDayNumber = <? php echo $day_of_week; ?> ;
                    var noDays = <? php echo $totalday; ?> ;
                    var currentMonth = <? php echo $month; ?>
                    var i = 1;
                    $("caption").html(currentMonth);

                    $('table tr').each(function() {
                        var $row = $(this);
                        $row.children().each(function() {

                            var $cell = $(this);

                            if (i >= firstDayNumber && i <= noDays) {
                                $cell.html(i);
                            } else {
                                $cell.addClass("closed");
                            }

                            ++i;
                        });
                    });
                });
            });
        </script>
        <style>
            table {
                position: relative;
                border: 1px solid black;
                height: auto;
                width: auto;
                table-layout: fixed;
                border-collapse: collapse;
            }
            td {
                display: inline;
                border: 1px solid black;
                position: static;
                text-align: center;
                font-size: 1.5em;
                padding: 12px 20px;
            }
        </style>
    </head>

    <body>
        <table id="tbl">
            <caption style="font-size: 1.7em"></caption>
            <br>
            <tr>
                <th>MON</th>
                <th>TUE</th>
                <th>WED</th>
                <th>THU</th>
                <th>FRI</th>
                <th>SAT</th>
                <th>SUN</th>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
            </tr>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <tr>
                    <td></td>
                    <td></td>
                </tr>
            </tr>
        </table>
        <button>Generate</button>
    </body>

1 个答案:

答案 0 :(得分:0)

嘿,你的代码几乎没有语法错误(第21,22和23行[PHP语法错误])。然后第23行出现了javascript语法错误。 更正后的代码如下。

    <?php
    date_default_timezone_set('Asia/Kolkata');

    $inputMonth = date('Y-m-d');

    $totalday = date("t", strtotime($inputMonth)); 
    $month = date("m" , strtotime($inputMonth));
    $year = date("Y" , strtotime($inputMonth));
    $getdate = getdate(mktime(null, null, null, $month, 1, $year));
    $first_day = $getdate["weekday"];   
    $day_of_week = date('N', strtotime($first_day));        
?>

<html>
<head>
    <title>Calendar Test</title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("button").click(function() {
                var firstDayNumber = <?php echo $day_of_week; ?> ;
                var noDays = <?php echo $totalday; ?> ;
                var currentMonth = <?php echo $month; ?>;
                var i = 1;
                $("caption").html(currentMonth);

                $('table tr').each(function() {
                    var $row = $(this);
                    $row.children().each(function() {

                        var $cell = $(this);

                        if (i >= firstDayNumber && i <= noDays) {
                            $cell.html(i);
                        } else {
                            $cell.addClass("closed");
                        }

                        ++i;
                    });
                });
            });
        });
    </script>
    <style>
        table {
            position: relative;
            border: 1px solid black;
            height: auto;
            width: auto;
            table-layout: fixed;
            border-collapse: collapse;
        }
        td {
            display: inline;
            border: 1px solid black;
            position: static;
            text-align: center;
            font-size: 1.5em;
            padding: 12px 20px;
        }
    </style>
</head>

<body>
    <table id="tbl">
        <caption style="font-size: 1.7em"></caption>
        <br>
        <tr>
            <th>MON</th>
            <th>TUE</th>
            <th>WED</th>
            <th>THU</th>
            <th>FRI</th>
            <th>SAT</th>
            <th>SUN</th>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <tr>
                <td></td>
                <td></td>
            </tr>
        </tr>
    </table>
    <button>Generate</button>
</body>