【发布时间】:2016-01-14 17:10:55
【问题描述】:
首先,让我告诉你我想要做什么。我正在尝试做一个日期选择器,当用户单击日期时,将显示特定日期的信息。
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);
?>
现在的问题是,日期选择器似乎很好。但是当我点击日期时,没有数据被显示出来。我有一种感觉,是我的 php 导致了问题。如果有人能给我一些关于我应该如何写它的指导……我真的很感激。非常感谢。
【问题讨论】:
-
这超出了主题,但您的代码是否安全免受 SQLInjection 影响?
-
不,不是。如果它与主题相关,那就太好了......
-
在
my_ajax_stuff.php中,您使用的是$_post['dateText']而不是$_post['date'],您应该在calendar.html文件中添加一个带有id="in"的元素。无论如何,你导入了太多的 js 和 css 文件,你只需要jquery.js、jquery-ui.min.js和jquery-ui.min.css。您只需要一次 datepicker 的初始化代码,也许您可以在$(document).ready(function(){})中执行此操作。
标签: php datepicker jquery-ui-datepicker