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 datatypeint
ordatetime
– Narendrasingh Sisodia Commented Mar 3, 2015 at 6:09
2 Answers
Reset to default 3In 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条)