我接到一项任务,使用 datepicker、ajax、php 和 mysql 在两个给定日期之间生成报告。下面是我的 html:
按日期报告
From date: <input type="text" id="fromdate" value=""> To date: <input type="text" id="todate" value="" onChange="showuser(document.getElementById('fromdate').value,document.getElementById('todate').value)">
<br>
<div id="txtHint"><b>User informathions will be listed here.</b></div>
脚本:
<script>
$(function() {
$( "#fromdate" ).datepicker();
$( "#todate" ).datepicker();
});
</script>
<script type="text/javascript">
function showUser(fromdate,todate)
{
if (fromdate =="" && todate=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","bet_date.php?fromdate="+fromdate+"&todate="+todate,true);
xmlhttp.send();
}
</script>
这是应该生成报告的 php 文件: bet_date.php
include("database.php");
$fromdate=$_GET["fromdate"];
$todate=$_GET["todate"];
$sql = "SELECT * FROM bookings WHERE date between '$fromdate' and '$todate'";
$result = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>date</th>
<th>start</th>
<th>name</th>
<th>email</th>
<th>phone</th>
<th>comments</th>
<th>approved</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['start'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
问题是当我选择两个日期时什么也没有发生。 在这种情况下请帮助我。 简单的例子将不胜感激。 谢谢。
最佳答案
I have Created Whole Tutorial for Date Wise Report, so once try it
表结构
CREATE TABLE IF NOT EXISTS `bookings` (
`id` int(12) NOT NULL AUTO_INCREMENT,
`date` date NOT NULL,
`start` varchar(255) NOT NULL,
`name` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
`comments` longtext NOT NULL,
`approved` varchar(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `bookings` (`id`, `date`, `start`, `name`, `email`, `phone`, `comments`, `approved`) VALUES
(1, '2014-12-17', 'yes', 'Mahendra', 'mahendra@XXXX', '89XXXXXXXX', 'nothing', 'yes'),
(2, '2014-12-18', 'no', 'Rahul', 'rahul@XXXXXX', '987XXXXXXX', 'very nice article', 'yes');
my_test.php
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#generate_report").click(function(){
var from_date=jQuery("#fromdate").val();
var to_date=jQuery("#todate").val();
var data =
{
from_date : from_date,
to_date : to_date
}
jQuery.ajax({
type: "POST",
url: "test.php",
data: data,
success: function(responce){
$("#txtHint").after(responce);
}
});
});
});
</script>
From date: <input type="text" id="fromdate" value="2014-12-17"> To date: <input type="text" id="todate" value="2014-12-18">
<input type="button" name="generate_report" id="generate_report" value="Generate Report" />
<br>
<div id="txtHint"><b>User informations will be listed here.</b></div>
test.php(ajax调用文件)
<?php
error_reporting(0);
include_once("wp-config.php");
extract($_POST);
$sql1=mysql_query("Select * from bookings where date between '".$from_date."' AND '".$to_date."'");
echo "<table border='1'>
<tr>
<th>id</th>
<th>date</th>
<th>start</th>
<th>name</th>
<th>email</th>
<th>phone</th>
<th>comments</th>
<th>approved</th>
</tr>";
while($row = mysql_fetch_array($sql1))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['start'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['comments'] . "</td>";
echo "<td>" . $row['approved'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
这个演示工作正常,我刚刚创建了,所以一旦检查它...
请任何人感兴趣,请正确回答并为此解决方案投票。 谢谢
关于php - 如何使用datepicker、ajax、php、mysql生成两个日期之间的报告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27544555/