PHP / AJAX:过滤日期范围后不显示数据

问题描述 投票:0回答:1

目前,我的代码有问题。我为过滤日期范围创建了一个系统。在进行数据过滤之前,它将显示所有数据,但是当我输入日期范围并单击按钮时,尽管我输入了正确的日期范围,但仍将显示“未找到已购买的物品”。下面是我的代码

index.php

    <?php

        require_once "configPDO.php";
        $query = "SELECT * FROM booking ORDER BY Book_No DESC";
        $sql = $conn->prepare($query);
        $sql -> execute();

    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Date Range Search with jQuery DatePicker using Ajax, PHP & MySQL | softAOX Tutorial</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"/>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css"/>
    </head>
    <body>
        <br/>
        <div class="container">
            <h2 align="center">Date Range Search with jQuery DatePicker using Ajax, PHP & MySQL</h2>
            <br/>
            <br/>
            <div class="col-md-2">
                <input type="text" name="From" id="From" class="form-control" placeholder="From Date"/>
            </div>
            <div class="col-md-2">
                <input type="text" name="to" id="to" class="form-control" placeholder="To Date"/>
            </div>
            <div class="col-md-8">
                <input type="button" name="range" id="range" value="Range" class="btn btn-success"/>
            </div>
            <div class="clearfix"></div>
            <br/>
            <div id="purchase_order">
                <table class="table table-bordered">
                    <tr>
                        <th width="25%">Start</th>
                        <th width="25%">End</th>
                        <th width="30%">Purpose</th>
                        <th width="10%">Remark</th>
                        <th width="10%">Room ID</th>
                    </tr>
                    <?php
                    while($row = $sql->fetch(PDO::FETCH_ASSOC))
                    {
                    ?>
                        <tr>
                            <td><?php echo $row["StartTime"]; ?></td>
                            <td><?php echo $row["EndTime"]; ?></td>
                            <td><?php echo $row["Meeting_Description"]; ?></td>
                            <td><?php echo $row["Remark"]; ?></td>
                            <td><?php echo $row["Room_ID"]; ?></td>
                        </tr>
                    <?php
                    }
                    ?>
                </table>
            </div>
        </div>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.js"></script>
        <!-- Script -->
        <script>
        $(document).ready(function(){
            $.datepicker.setDefaults({
                dateFormat: 'yy-mm-dd'
            });
            $(function(){
                $("#From").datepicker();
                $("#to").datepicker();
            });
            $('#range').click(function(){
                var From = $('#From').val();
                var to = $('#to').val();
                if(From != '' && to != '')
                {
                    $.ajax({
                        url:"range.php",
                        method:"POST",
                        data:{From:From, to:to},
                        success:function(data)
                        {
                            $('#purchase_order').html(data);
                        }
                    });
                }
                else
                { 
                    alert("Please Select the Date");
                }
            });
        });
        </script>
    </body>
    </html>

range.php

<?php

require_once "configPDO.php";

if(isset($_POST["From"], $_POST["to"]))
{
    $result = '';
    $query = "SELECT * FROM booking WHERE Book_Date BETWEEN '".$_POST["From"]."' AND '".$_POST["to"]."'";
    $sql = $conn->prepare($query);
    $sql -> execute();
    $result .='
    <table class="table table-bordered">
    <tr>
    <th width="25%">Start</th>
    <th width="25%">End</th>
    <th width="30%">Purpose</th>
    <th width="10%">Remark</th>
    <th width="10%">Room ID</th>
    </tr>';
    if($sql->rowCount() > 0)
    {
        while($row = $sql->fetch(PDO::FETCH_ASSOC))
        {
            $result .='
            <tr>
            <td>'.$row["StartTime"].'</td>
            <td>'.$row["EndTime"].'</td>
            <td>'.$row["Meeting_Description"].'</td>
            <td>'.$row["Remark"].'</td>
            <td>'.$row["Room_ID"].'</td>
            </tr>';
        }
    }
    else
    {
        $result .='
        <tr>
        <td colspan="5">No Purchased Item Found</td>
        </tr>';
    }
    $result .='</table>';
    echo $result;
}
?>
php ajax pdo
1个回答
0
投票

我会要求您向我展示数据库的结构,以确定您是否遗漏了任何字段名称或匹配了错误的日期格式。如果您提供的话,我也许可以为您提供帮助。

热门问题
推荐问题
最新问题