从jQuery DatePicker恢复数据不起作用

时间:2016-05-06 20:48:24

标签: jquery datepicker

从jQuery DatePicker恢复数据不起作用。 $ dateDebut和$ dateFin是空的..我不明白为什么。我从#from和#to恢复了数据,但我的2回声测试不起作用。



<script>
  $(function() {

    

    $( "#from" ).datepicker({
      defaultDate: "+1d",
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat : '@',
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 1,
      dateFormat : '@',
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
        var currentDate = $( ".selector" ).datepicker( "getDate" );
      }
    });
  });
  </script>
 
<label for="from">Du</label>
<input type="text" id="from" name="from">
<label for="to">Au</label>
<input type="text" id="to" name="to">

<?php
  $dateDebut = $_POST[‘from’] ;
  $dateFin = $_POST[‘to’] ;
  echo('La date est : ' . $dateDebut);
  echo($dateFin);
?>
&#13;
&#13;
&#13;

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您将日期选择器<input>元素包含在<form>中,并将操作设置为同一页面,则可以在提交表单后检查同一页面上的$_POST变量。如果没有提交,PHP将无法获取变量。如果您尝试在服务器端脚本中使用日期而不提交/刷新页面,您也可以查看AJAX。

以下是如何在一个文件中进行设置的方法:

<!doctype html>
<html>
  <head>
    <title>Page Title Here</title>
    <link rel='shortcut icon' href='favicon.ico'>
    <!-- include stylesheet for jquery ui -->
    <link rel='stylesheet' href='https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css'>
  </head>
  <body>
    <?php
      //check if the dates have been posted
      //assign the dates to variables if they've been posted,
      //otherwise assign them the value of false
      $dateDebut = isset($_POST['from'])?$_POST['from']:false;
      $dateFin = isset($_POST['to'])?$_POST['to']:false;
    ?>

    <!-- the form which contains our datepickers -->
    <form action='./' method='POST'>
      <label for='from'>Du</label>
      <input name='from' id='from'>
      <label for='to'>Au</label>
      <input name='to' id='to'>
      <input type='submit' value='Submit'>
    </form>

    <?php
      //print the dates if they were posted (if they aren't set to false)
      if(false !== $dateDebut){
        //start date ms timestamp
        echo 'start date timestamp (ms) : '.$dateDebut."<br>";
        //start date formatted
        echo 'start date (date format - mm/dd/yyyy): '.date("m/d/Y", ($dateDebut / 1000))."<br>";
      }
      if(false !== $dateFin){
        //end date timestamp
        echo 'end date timestamp (ms) : '.$dateFin."<br>";
        //end date formatted
        echo 'end date (date format - mm/dd/yyyy): '.date("m/d/Y", ($dateFin / 1000))."<br>";
      }
    ?>
    <!-- include jquery and jquery-ui -->
    <script src="https://code.jquery.com/jquery-1.12.3.min.js" integrity="sha256-aaODHAgvwQW1bFOGXMeX+pC4PZIPsvn2h1sArYOhgXQ=" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw=" crossorigin="anonymous"></script>
    <script>
      //setup datepickers
      $('[name=from]').datepicker({
        defaultDate: "+1d",
        changeMonth: true,
        numberOfMonths: 1,
        dateFormat : '@',
        onClose: function( selectedDate ) {
          $( "#to" ).datepicker( "option", "minDate", selectedDate );
        }
      });
      $('[name=to]').datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        numberOfMonths: 1,
        dateFormat : '@',
        onClose: function( selectedDate ) {
          $( "#from" ).datepicker( "option", "maxDate", selectedDate );
          var currentDate = $( ".selector" ).datepicker( "getDate" );
        }
      });
    </script>
  </body>
</html>