使用ajax和php从mysql db获取数据,并使用html5 datepicker进行过滤

时间:2014-09-30 11:57:37

标签: mysql ajax date filter datepicker

我是stackoverflow的新手。我有一个问题,我希望你能提供帮助。 所以我有一个mysql数据库,我想从中获取数据。我用ajax调用创建了一个php程序,但没有使用ajax。

请帮助我们,我现在学习php,mysql和ajax。

这是index.php的代码:

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    </head>
    <body>
    <div>
            <script type="text/javascript">

             $(document).ready(function() {

                //##### send add record Ajax request to response.php #########
                $("#display").click(function (e) {

                e.preventDefault();

                    var myData = 'datepicker='+ $("#datepicker").val(); 
                  $.ajax({    //create an ajax request to load_page.php
                    type: "GET",
                    url: "file2.php",             
                    dataType: "text",   //expect html to be returned
                    data: myData,
                    success: function(response){                    
                        $("#result").html(response); 
                        //alert(response);
                    }

                });
            });
            });

            </script>       
            <h4>Válaszd ki a megjelenítendő napot!<h4>
            <form >
            <input type="date" name="datepicker" id="datepicker"/>
            <input type="submit" id="display" value="Submit" />
            </form>
            <div id="result"></div>


        </div>

    </body>
   </html>

和action.php

<?php

// Make a MySQL Connection
########## MySql details (Replace with yours) #############
$username = "root"; //mysql username
$password = ""; //mysql password
$hostname = "localhost"; //hostname
$databasename = ''; //databasename

//connect to database
$mysqli = new mysqli($hostname, $username, $password, $databasename);

if(!empty($_POST['datepicker'])){ 
$date = $_POST['datepicker']; 

// Get all the data from the "example" table
$result = $mysqli->query("SELECT * FROM table WHERE date = '{$date}'"); // 

while($row = $result->fetch_assoc()) { ?>
          <ul class="ntrtnlist" id="item_<?php echo $row["id"] ?>">
              <li><?php echo $row["date"] ?></li>
              <li><?php echo $row["time"] ?></li>
              <li><?php echo $row["mealtime"] ?></li>
              <li><?php echo $row["name"] ?></li>
              <li><?php echo $row["quantity"] ?>g</li>
              <li class="del_wrap"><div class="del_wrapper">
                <a href="#" class="del_button" id="del-<?php echo $row["id"] ?>">
                <img src="images/icon_del.svg" border="0" width="16" height="16" />
                </a>
                </div>
              </li>
          </ul>



<?php
}
}
 //close db connection
$mysqli->close();

?>

0 个答案:

没有答案