我编写了基于月份选择显示数据的代码。月份正在改变,但数据没有显示。请参阅我的代码一次并建议我该怎么做。当点击下一个代码时,它会更改月份,但日历中的数据不会显示。
<!DOCTYPE html>
<html>
<body>
<form method="post" action="display.php">
<!--<button id="prev">-->
<input type="button" id="prev" name="prev" value="prev" />
<input type="text" id="date" name="date" value=""
style="border:none;" />
<input type="button" id="next" name="next" value="next" />
<!--<button id="next">-->
<script>
//document.getElementById("date").value = new Date().toJSON().slice(0,10)
var today = new Date();
var mm=today.getMonth();
var yy=today.getFullYear();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
var dat=monthNames[mm]+' '+yy;
var yyy="";
document.getElementById("date").value = dat;
document.getElementById('prev').addEventListener("click", function() {
var last=document.getElementById("date").value;
var val=last.split(" ");
var month=val[0];
var year=val[1];
if(month=='January'){
var mmm=11;
yyy=year-1;
var dat=monthNames[mmm]+' '+yyy;
document.getElementById("date").value = dat ;
}
else {
var index=monthNames.indexOf(month);
var mmm=index-1;
var dat=monthNames[mmm]+' '+year;
document.getElementById("date").value = dat;
}
});
var xxx="";
document.getElementById('next').addEventListener("click", function() {
var next=document.getElementById("date").value;
var val=next.split(" ");
var month=val[0];
var year=val[1];
if(month=='December'){
var mmm=0;
xxx=++year;
var dat=monthNames[mmm]+' '+xxx;
document.getElementById("date").value = dat ;
}
else {
var index=monthNames.indexOf(month);
var mmm=index+1;
var dat=monthNames[mmm]+' '+year;
document.getElementById("date").value = dat;
}
});
</script>
</form>
</body>
</html>
Display.php的
<?php
include 'config.php';
if(isset($_POST['prev']) || isset($_POST['next']))
{
$monyear=$_POST['date'];
$mnh=explode(' ',$monyear);
$years=$mnh[1];
$months=strtolower(substr("$mnh[0]",0,3));
$comb=$months.'-'.$years;
$select ="SELECT * FROM `daily_report` WHERE `reg_id`='akshaypm123' and
`monyr`='$comb' ";
$res = mysqli_query($db,$select);
while($row=mysqli_fetch_array($res))
{
$abc=$row['monyr'];
$count=0;
$ab=explode('-',$abc);
$year = $ab[1];
$month = $ab[0];
$mon = date("m", strtotime($month));
$date = mktime(12, 0, 0, $mon, 1, $year);
$daysInMonth = date("t", $date);
$offset = date("w", $date); //for may->offset=1
$rows = 1;
echo "<h1 align = center> " . date("F Y", $date) . "</h1>\n";
echo "<table align = center>";
echo "<tbody style = 'font-family:monospace;background-color:beige; border-
top:2 solid black'>";
echo "\t<tr style ='height:3em;background-color:mistyrose;'><th>Sunday</th>
<th>Monday</th><th>Tuesday</th><th>Wednesday</th><th>Thursday</th>
<th>Friday</th><th>Saturday </th></tr>";
echo "\n\t<tr>";
for($i = 1; $i <= $offset; $i++)
{
echo "<td border = 2 soild black></td>";
}
for($day = 1; $day <= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "<tr>\n\t</tr>";
$rows++;
}
$cba=$row['d'.$day.''];
$time=explode('-',$cba);
if ($cba=='leave') {
$count +=1;
}
if($count>1)
{
switch($time[0]) {
case 'leave':
$bg_color = "red";
break;
/* case 'absent':
$bg_color = "red";
break;*/
case 'NH':
$bg_color = "yellow";
break;
case $time[0]=='08:30 AM ' || $time[0]<='09:00 AM ':
$bg_color = "honeydew";
break;
case $time[0]>'09:00 AM ':
$bg_color = "orange";
break;
}
}
else {
switch($time[0]) {
case 'leave':
$bg_color = "green";
break;
case 'NH':
$bg_color = "yellow";
break;
case $time[0]=='08:30 AM ' || $time[0]<='09:00 AM ':
$bg_color = "honeydew";
break;
case $time[0]>'09:00 AM ':
$bg_color = "orange";
break;
}
}
if(($day + $offset - 1) % 7 == 0){
echo "<td style=' width:6em; background-
color:cornflowerblue'>  <br></td>";
}
else if($time[0]!=null){
echo "<td style=' width:10em; height:5em; text-align:center;
background-color:".$bg_color."'>".$cba."</td>";
}
else {
echo "<td style='background-color:red;text-align:center;'>LOP<br>
</td>";
}
}
while( ($day + $offset) <= $rows * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr>";
echo "</tbody></table>";
}
}
else{
$disp= strtolower(date("M-Y"));
$select ="SELECT * FROM `daily_report` WHERE `reg_id`='akshaypm123' and
`monyr`='$disp'";
$res = mysqli_query($db,$select);
while($row=mysqli_fetch_array($res))
{
$abc=$row['monyr'];
$count=0;
$ab=explode('-',$abc);
$year = $ab[1];
$month = $ab[0];
$mon = date("m", strtotime($month));
$date = mktime(12, 0, 0, $mon, 1, $year);
$daysInMonth = date("t", $date);
$offset = date("w", $date); //for may->offset=1
$rows = 1;
echo "<h1 align = center> " . date("F Y", $date) . "</h1>\n";
echo "<table align = center>";
echo "<tbody style = 'font-family:monospace;background-color:beige;
border-top:2 solid black'>";
echo "\t<tr style ='height:3em;background-color:mistyrose;'>
<th>Sunday</th><th>Monday</th><th>Tuesday</th><th>Wednesday</th>
<th>Thursday</th><th>Friday</th><th>Saturday </th></tr>";
echo "\n\t<tr>";
for($i = 1; $i <= $offset; $i++)
{
echo "<td border = 2 soild black></td>";
}
for($day = 1; $day <= $daysInMonth; $day++)
{
if( ($day + $offset - 1) % 7 == 0 && $day != 1)
{
echo "<tr>\n\t</tr>";
$rows++;
}
$cba=$row['d'.$day.''];
$time=explode('-',$cba);
if ($cba=='leave') {
$count +=1;
}
if($count>1)
{
switch($time[0]) {
case 'leave':
$bg_color = "red";
break;
case 'NH':
$bg_color = "yellow";
break;
case $time[0]=='08:30 AM ' || $time[0]<='09:00 AM ':
$bg_color = "honeydew";
break;
case $time[0]>'09:00 AM ':
$bg_color = "orange";
break;
}
}
else {
switch($time[0]) {
case 'leave':
$bg_color = "green";
break;
case 'NH':
$bg_color = "yellow";
break;
case $time[0]=='08:30 AM ' || $time[0]<='09:00 AM ':
$bg_color = "honeydew";
break;
case $time[0]>'09:00 AM ':
$bg_color = "orange";
break;
}
}
if(($day + $offset - 1) % 7 == 0){
echo "<td style=' width:6em; background-
color:cornflowerblue'>  <br></td>";
}
else if($time[0]!=null){
echo "<td style=' width:10em; height:5em; text-align:center;
background-color:".$bg_color."'>".$cba."</td>";
}
else {
echo "<td style='background-color:red;text-align:center;'>LOP<br>
</td>";
}
}
while( ($day + $offset) <= $rows * 7)
{
echo "<td></td>";
$day++;
}
echo "</tr>";
echo "</tbody></table>";
}
}
?>
我通过传递日期值尝试了ajax,但它无效。enter image description here