簡體   English   中英

jQuery Datepicker值並存儲到php變量中

[英]jQuery Datepicker value and storing into a php variable

我正在嘗試將jQuery datepicker值存儲到php日期格式的變量中。

預期結果:示例

輸入字段名稱“ reviewdate” = 2015年3月2日

輸入字段名稱“ reviewmonth” = 3

這兩個值都將被提交到mysql數據庫。 我可以存儲“ reviewdate”,但是在信息提交到數據庫之后,“ reviewmonth”仍然為“ null”。

<body>

   <!-- Datepicker -->
   <h2 class="demoHeaders">Datepicker</h2>

   <input name="reviewdate" type="text" id="datepicker"></input>

   <input name="reviewmonth" type="hidden"  value="<?php if(isset($_GET["reviewdate"])) { echo date_format($_GET["reviewdate"], "n");} else {echo "";} ?>"></input>

   <script src="external/jquery/jquery.js"></script>
   <script src="jquery-ui.js"></script>
   <script>
      $( "#datepicker" ).datepicker({
         inline: true
      });
  </script>
</body?

Thank you so much for all your help!

為了使date_format起作用,您需要首先初始化DateTime對象:

<?php 
$month = '';
if(isset($_GET["reviewdate"])) { 
    $date = new DateTime($_GET["reviewdate"]); // or $date = date_create($_GET["reviewdate"]); will work the same
    $month = date_format($date, "n");
}
?>
<input name="reviewmonth" type="hidden" value="<?php echo $month; ?>"></input>

如果要在JS中執行此操作,則需要為datepicker添加一個處理程序以處理reviewmonth,並將其附加到隱藏的輸入中:

<?php

if(isset($_POST['submit'])) {
    echo '<pre>', print_r($_POST), '</pre>';
}

?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" />
<form method="POST">
    <input name="reviewdate" type="text" id="datepicker" />
    <input name="reviewmonth" type="hidden" value="" />
    <input type="submit" name="submit" />
</form>

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.11.3/jquery-ui.js"></script>
<script type="text/javascript">
$('#datepicker').datepicker({
    inline: true,
    onSelect: function(dateText, inst){
        var date = $(this).val();
        var d = new Date(date);
        var n = (d.getMonth() + 1);
        $('input[name="reviewmonth"]').attr('value', n);
    }
});
</script>

樣本輸出

你必須替換這個

 <script>
  $( "#datepicker" ).datepicker({
     inline: true
  });
  </script>

 <script>
  $( "#datepicker" ).datepicker({
     inline: true,
    dateFormat: 'Y-m-d'
  });
 </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM