cole71
cole71

Reputation: 5

jquery datepicker refreshing data on page

This is a two fold question. How do I update the data (without page reload) on the page when the datepicker is changed? Also how do i pass the selected date to the dtpickerdate in my sql statement? Here is my example code

<html>
<head>
<link href="../css/jquery/jquery-ui.css" rel="stylesheet" type="text/css" />
  <script src="../js/jquery-1.9.1.js"></script>
  <script src="../js/jquery-ui.js"></script>
</head>
<body>
<?php include("../navbar.php"); ?>
<p>Date: <input type="text" id="datepicker" value="<?php echo date("m/d/Y"); ?>" /></p>
<script type="text/javascript">
$(function() {
  $("#datepicker").datepicker();
});
</script>
<table>
<?php 

$sqltext = "SELECT * FROM data where startdate=dtpickerdate";

$result = $mysqli->query($sqltext);
while($row = $result->fetch_assoc()) {
  echo "<tr><td>".$key."</td>";
  echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td><td>".$row['wed']."</td><td>".
          $row['thurs']."</td><td>".$row['fri']."</td><td>&nbsp".$row['sat']."</td>";

}}
?>
</table>
</body>
</html>

Upvotes: 0

Views: 1819

Answers (1)

omma2289
omma2289

Reputation: 54619

You need AJAX, first thing create a new php file with your query function that will receive a $_POST parameter with the value of the datepicker:

getData.php

<?php 
    $dtpickerdate = isset($_POST['dtpickerdate']) ? $_POST['dtpickerdate'] : NULL
    $sqltext = "SELECT * FROM data where startdate = $dtpickerdate";

    $result = $mysqli->query($sqltext);
    while($row = $result->fetch_assoc()) {
      echo "<tr><td>".$key."</td>";
      echo "<td>".$row['sun']."</td><td>".$row['mon']."</td><td>".$row['tues']."</td> <td>".$row['wed']."</td><td>".$row['thurs']."</td><td>".$row['fri']."</td><td>&nbsp".$row['sat']."</td>";
    }
?>

Now you listen to the change event on your input and use AJAX to call your PHP, when the call completes you update your table:

$('#datepicker').change(function(){
    $.post('getData.php',{dtpickerdate : $(this).val()}, function(response){
        $('table').html(response);
    });
});

Upvotes: 0

Related Questions