我正在尝试从用户选择两个日期时显示数据库中的数据。第一个日期将是“开始日期”,第二个日期将是“结束日期”根据我给出的规格,他们需要查看用户输入日期之间的数据。当用户搜索当月的特定日期并且数据显示没有问题时,我目前正在使用它。我遇到的问题是当他们输入搜索的开始和结束日期时显示数据。如果有人能看到我的错误或缺少我需要的代码,我将非常感激。谢谢。
这是我的HTML文件
$( "#datepicker , #datepicker2" ).datepicker({ dateFormat: "yy-mm-dd" });
</script>
<html>
<body>
<div id="Search-Form">
<div id="search-container">
<label>
<span>Search :</span>
<input type="text" name="search" value="" id="datepicker" class="text-field">
<span>or Between</span>
<input type="text" name="search2" value="" id="datepicker2" class="text-field">
</label>
<div id="search-submit">
<input type="button" name="submit" id="sumbit" value="SUBMIT" onclick="loadData()">
</div>
</div>
</div>
<div id="queryTable">
</div>
<div id="queryTractors">
</div>
</body>
</html>
这是我的ajax.php文件
include "../Database/connection.php";
$tractor_Tbl = "Tractor";
$trailer_Tbl = "Trailer";
$data = array();
$TractorData = array();
if(isset($_REQUEST['search'])) {
$se = mysql_entities_fix_string($connection, $_REQUEST['search']);
$se2 = mysql_entities_fix_string($connection, $_REQUEST['search2']);
$query = "SELECT * FROM Trailer WHERE datewashed BETWEEN '$se' AND '$se2'";
$result = $connection->query($query);
while($info = mysqli_fetch_array($result)) {
array_push($data , $info);
}else{
echo $connection->error;
}
}
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo json_encode ($data);
return;
function mysql_fix_string($connection, $string) {
if(get_magic_quotes_gpc()) $string = stripslashes($string);
return $connection->real_escape_string($string);
}
function mysql_entities_fix_string($connection, $string) {
return htmlentities(mysql_fix_string($connection, $string));
}
?>
这是我目前拥有的加载功能。
function loadData() {
// Search val
var search = $("input#datepicker").val();
var search2 = $("input#datepicker2").val();
var url = encodeURI('/BLT/Services/ajax.php?search=' + search+'&search2=' + search2);
$.getJSON( url, function( data ) {
var html = '';
html += "<table border='1' border-color='black' cellpadding='2px'><thead>";
html += "<tr height='43' align='center'>";
html += "<th cellpadding='2px'><b>Trailer Number</b></th>";
html += "<th cellpadding='2px'><b>Location</b></th>";
html += "<th cellpadding='2px'><b>Washer</b></th>";
html += "<th cellpadding='2px'><b>Exterior Wash</b></th>";
html += "<th cellpadding='2px'><b>Date Washed</b></th>";
html += '</tr><tbody>';
var count = 0;
$.each( data, function( index, row ) {
html += '<tr>';
html += "<td cellpadding='2px'>" + row.Trailer_Num + "</td>";
html += "<td cellpadding='2px'>" + row.Location + "</td>";
html += "<td cellpadding='2px'>" + row.washer + "</td>";
html += "<td cellpadding='2px'>" + row.exWash + "</td>";
html += "<td cellpadding='2px'>" + row.datewashed + "</td>";
html += '</tr>';
count++;
});
if (count == 0) {
html += '<tr><td colspan="5">No results were found from your query.</td></tr>';
}
html += '</tbody></table>';
$("div#queryTable").html(html);
});
}
答案 0 :(得分:0)
为什么不使用BETWEEN
$query = "SELECT * FROM Trailer WHERE datewashed >= '$se' AND datewashed <= '$se2'";
改变它
$query = "SELECT * FROM Trailer WHERE datewashed BETWEEN '$se' AND '$se2'";
答案 1 :(得分:0)
function loadData()只发送datePicker的值;而不是&#39; datePicker2&#39;的价值。 我想这可能是问题所在。我不知道PHP。您能否在PHP方面验证您是否同时收到这两个日期?如果没有,那么我认为你应该重新安排你的JSON URL如下
encodeURI('/BLT/Services/ajax.php?search=' + search+'&search2=' + search2);
其中search2是
var search = $("input#datepicker2").val();
BTW,命名变量并不是很好。您可以将&#39; datepicker命名为&#39; datepicker&#39;和&#39; datepicker2&#39; as&#39; startDate&#39;并且&#39; endDate&#39;分别
答案 2 :(得分:0)
你能准确地告诉我们你在回复中得到了什么吗?你可以调试如下吗?