PHP datepicker日历

时间:2016-01-14 17:10:56

标签: php datepicker jquery-ui-datepicker

首先,让我告诉你我想做什么。我正在尝试做一个日期选择器,当用户点击日期时,会显示有关特定日期的信息。

calendar.html

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>jQuery UI Datepicker - Default functionality</title>
    <link rel="stylesheet" href="jquery-ui.css">
    <link rel="stylesheet" href="jquery-ui.min.css">
    <link rel="stylesheet" href="jquery-ui.structure.css">
    <link rel="stylesheet" href="jquery-ui.structure.min.css">
    <link rel="stylesheet" href="jquery-ui.theme.css">
    <link rel="stylesheet" href="jquery-ui.theme.min.css">

    <script src="jquery.js"></script>
    <script src="jquery-ui.js"></script>
    <script src="jquery-ui.min.js"></script>
    <link rel="stylesheet" href="/resources/demos/style.css">
    <script>
    $(function() {
        $( "#datepicker" ).datepicker();
    });
    $( "#datepicker" ).datepicker({
        minDate: 0, 
        maxDate: 30, //The maximal date that can be selected, i.e. + 1 month, 1 week, and 3 days from "now"
        showAnim: "bounce",
        onSelect: function(dateText, inst) {
            $.ajax({
                type: 'POST',
                url: 'my_ajax_stuff.php',
                data: {date : dateText},
                success: function(response){
                    document.getElementById("in").innerHTML = response;
                }
            });
        }
    });

    </script>

</head>
<body>

    <p>Date: <input type="text" id="datepicker"></p>

</body>
</html>

这是我的 my_ajax_stuff.php

<?php
$connection = mysqli_connect("localhost","root","","test");
if (!$con) {
    die('Could not connect: ' . mysqli_error($connection));
}

$sql = "SELECT * FROM calendar WHERE startdate={$_post['dateText']}";

$result = mysqli_query($connection,$sql);
echo "<table>
<tr>
<th>title</th>
<th>startdate</th>
<th>enddate</th>
</tr>
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['startdate'] . "</td>";
    echo "<td>" . $row['enddate'] . "</td>";
    echo "</tr>";
}
echo "</table>";


mysqli_close($connection);
?>

现在的问题是,datepicker看起来似乎很好。但是当我点击日期时,没有数据被显示出来。我有一种感觉,这是我的PHP导致问题。如果有人能给我一些关于如何写它的方向......我真的很高兴。非常感谢。

1 个答案:

答案 0 :(得分:0)

仔细查看您的PHP代码,您应该能够在第一个echo之后发现缺少的双引号。这是一个可以帮助您前进的更正版本。

<?php

$connection = mysqli_connect("localhost","root","","test");
if (!$con) {
    die('Could not connect: ' . mysqli_error($connection));
}

$sql = "SELECT * FROM calendar WHERE startdate={$_post['dateText']}";

$result = mysqli_query($connection,$sql);
echo "<table>
<tr>
<th>title</th>
<th>startdate</th>
<th>enddate</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>" . $row['title'] . "</td>";
    echo "<td>" . $row['startdate'] . "</td>";
    echo "<td>" . $row['enddate'] . "</td>";
    echo "</tr>";
}
echo "</table>";
?>