简体   繁体   English

ASK-jQuery Datepicker-在文本输入中保留所选日期

[英]ASK - jquery datepicker - keep selected date in text input

I made a page which in html, php and jquery. 我做了一个页面,其中有html,php和jquery。 My php script will load all data where date is equals with date selected in text input which using JQuery datepicker. 我的PHP脚本将加载所有日期等于使用JQuery datepicker在文本输入中选择的日期的数据。

My problem is when I selected a date in datepicker and then the page will load the data, the text input at datepicker being blank (no text or date). 我的问题是,当我在日期选择器中选择一个日期,然后页面将加载数据时,在日期选择器中输入的文本为空白(无文本或日期)。

so how to keep the selected date in the text input? 那么如何在文本输入中保留所选日期呢?

  <link rel="stylesheet" href="/resources/demos/style.css">

$(function() {
    $("#datepicker").datepicker({dateFormat: 'yy-mm-dd'});
});

$(document).ready(function() {
    $('#datepicker').change(function() {
        $('#calendar').submit();
    });
});

I need some advices to fix this. 我需要一些建议来解决此问题。 So thank you for your help. 因此,谢谢您的帮助。 And also I want to apologize if my topic is alike with another. 如果我的话题与另一个话题相似,我也要道歉。

Thanks for your help. 谢谢你的帮助。

Here is my code script 这是我的代码脚本

<center>
<form id="calendar" action="<?php echo $_SERVER['PHP_SELF'];?>" method="GET">
<div id="datepicker_start"></div>
<input type="hidden" id="datepicker" name="datepicker">
</form>
</center> 



<?php
$host = "localhost";
$dbname = "dbname";
$username = "root";
$password = "";
try {
 $conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
 $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $pe) {
 die("Could not connect to the database $dbname :" . $pe->getMessage());
}

if(isset($_GET["datepicker"])){
$datepicker = $_GET["datepicker"];
$datepicker2 = date('d-m-Y', strtotime($datepicker));
}else{
$datepicker = date('Y-m-d');
$datepicker2 = date('d-m-Y', strtotime($datepicker));
} 
 $stmt = $conn->prepare("my query");
 $stmt->bindParam(':date', $datepicker);
 $stmt->execute();

 $result = $stmt->fetchall();

 if (empty($result)) {
     echo "<center><h3>.:: Schedule <i>". $datepicker2 ." ::.</i></h1></center>";
     echo "<center><h1>.::No Data::.</h1></center>";
 }else{ 
     echo "<center><h3>.:: Schedule <i>". $datepicker2 ." ::.</i></h1></center>";
     echo 
    "<table>
    <tr>
    <th>Data 1</th>
    <th>Data 2</th>
    <th>Data 3</th>
    <th>Data 4</th>
    <th>Data 5</th>
    <th>Data 6</th>
    <th>Data 7</th>
    <th>Data 8</th>
    <th>Data 9</th>

    </tr>"
    ;

    foreach($result as $row)
    {
  echo "<tr>";
  echo "<td>" . $row['data 1'] . "</td>";
  echo "<td>" . $row['data 2'] . "</td>";
  echo "<td>" . $row['data 3'] . "</td>";
  echo "<td>" . $row['data 4'] . "</td>";
  echo "<td>" . $row['data 5'] . "</td>";  
  echo "<td>" . $row['data 6'] . "</td>";
  echo "<td>" . $row['data 7'] . "</td>";
  echo "<td>" . $row['data 8'] . "</td>";
  echo "<td>" . $row['data 9'] . "</td>";

}

  echo "</tr>";
  echo "</table>";
}
?>

Form Submit reloads The Page. 表单提交重新加载页面。 So you should send the Date from server script on form submit and set it on the input field. 因此,您应该在表单提交中从服务器脚本发送日期,并在输入字段中进行设置。 or you can use ajax 或者你可以使用ajax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM