javascript - jQuery Datepicker value and storing into a php variable - Stack Overflow

I am trying to store a jQuery datepicker value into a php date formatted variable. The desired oute: Ex

I am trying to store a jQuery datepicker value into a php date formatted variable.

The desired oute: Example

input field name "reviewdate" = 03/02/2015

input field name "reviewmonth" = 3

Both values will be submitted to a mysql database. I am able to store "reviewdate", but "reviewmonth" remains "null" after the information is submitted to the database.

<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!

I am trying to store a jQuery datepicker value into a php date formatted variable.

The desired oute: Example

input field name "reviewdate" = 03/02/2015

input field name "reviewmonth" = 3

Both values will be submitted to a mysql database. I am able to store "reviewdate", but "reviewmonth" remains "null" after the information is submitted to the database.

<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!
Share Improve this question edited Mar 3, 2015 at 7:32 Kevin 41.9k12 gold badges56 silver badges72 bronze badges asked Mar 3, 2015 at 6:05 Rob SandsRob Sands 653 silver badges10 bronze badges 2
  • just use the onselect event of datepicker plugin to get the selected date and set the value in hidden value. – Frebin Francis Commented Mar 3, 2015 at 6:09
  • Your input fieldname reviewmonth has which datatype int or datetime – Narendrasingh Sisodia Commented Mar 3, 2015 at 6:09
Add a ment  | 

2 Answers 2

Reset to default 3

In order for date_format to work, you need to initialize a DateTime object first:

<?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>

If you want to do this in JS, you need to add a handler for the datepicker to handle the reviewmonth and append it inside the hidden input:

<?php

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

?>
<link rel="stylesheet" href="//code.jquery./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./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery./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>

Sample Output

you have to replace this

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

with

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745187064a4615687.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信