jQuery DatePicker, how to show value in input field from database - php

can't find solution to this issue. So i have datepicker input field, it works fine, i can choose date and save it to database. But when i want to edit this saved value in edit_profile.php this field is empty, i need to see the user specified date. How to do it?
Datepicker function:
$(function() {
$( "#datepicker" ).datepicker({
dateFormat: "yy-mm-dd",
changeMonth: true,
changeYear: true
}).val('<?php echo $age;?>');
});
Input:
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="" />
what to do ?

Can't you just do this:-
<input name="datepicker" type="text" id="datepicker" class="date_picker" value="<?php echo $age;?>" >

I think you should use defaultDate option, for example
$( "#datepicker" ).datepicker({ dateFormat: "yy-mm-dd", changeMonth: true, changeYear: true, defaultDate: yourDate });

You can use SetDate or Defaultdate option of datepicker:
$('#dateselector').datepicker("setDate", new Date(2008,9,03) );

Related

Why is datepicker ui one week behind php date("W")

I'm working with php and datepicker for an application that requires the week number from a date. I just noticed that datepicker is one week behind php date("W"). I have a Fiddle showing the issue. Is there a way to correct this? Thanks for any insight.
JS:
<script>
$(document).ready(function () {
$( ".datepicker" ).datepicker({
showWeek: true,
dateFormat: 'yy-mm-dd'
});
});
</script>
PHP:
<?php echo 'Current week ' .date("W");?>
HTML:
<input name="date_task[]" class="datepicker" type="text" />
I was able to fix it by changing the first day of the week to monday.
<script>
$(document).ready(function () {
$( ".datepicker" ).datepicker({
showWeek: true,
dateFormat: 'yy-mm-dd',
firstDay: 1
});
});
</script>

jQuery with multi datepicker

I have dynamic form, Now when Input generated, there is 2 datepicker (FromDate, ToDate), and you can generate many of this tow inputs.
The Input come like this:
<input type="text" class="input-date displayDate complexField" name="Course[FromDate][1]" value="" />
<input type="text" class="input-date displayDate complexField" name="Course[ToDate][1]" value="" />
Each Input will generate jQuery for datepicker like this:
$(document).ready(function() {
{/literal}$(".input-date").datepicker({literal}{
dateFormat: dFormat,
showOn: 'button',
changeMonth: true,
changeYear: true,
minDate: new Date(1940, 1 - 1, 1),
maxDate: '+10y',
yearRange: '-99:+99',
onSelect: function(selected) {
$("[name^='Course[FromDate]']").datepicker("option","maxDate", selected)});
$("[name^='Course[ToDate]']").datepicker("option","minDate", selected)});
}
});
Now everything work fine, you can add many these two inputs and all of them with datepicker.
The Only problem is:
The option onSelect work fine only if you have only the first element (FromDate and ToDate) the ToDate can't be greater than FromDate and vice versa,
But when I add other elemnts which will be:
<input type="text" class="input-date displayDate complexField" name="Course[FromDate][2]" value="" />
<input type="text" class="input-date displayDate complexField" name="Course[ToDate][2]" value="" />
The onSelect will work wrong, it will work 4 together, I need it work with elements with array index (1) alone, and in elements with index number (2) works alone and so on.
Please advice me on this ...
Use this code and try agin
$('document').ready(function(){
$('.input-date').on('click',function(){
$(this).datepicker({literal}{
dateFormat: dFormat,
showOn: 'button',
changeMonth: true,
changeYear: true,
minDate: new Date(1940, 1 - 1, 1),
maxDate: '+10y',
yearRange: '-99:+99',
onSelect: function(selected) {
$("[name^='Course[FromDate]']").datepicker("option","maxDate", selected)});
$("[name^='Course[ToDate]']").datepicker("option","minDate", selected)});
}
});
});

how Jquery UI Datepicker value set when redirect to this page?

When i submitted form than if error occurred than value is not set in datepicker which is selected before. Datepicker field empty . how this possible ??
<html>
<head>
<script type="text/javascript" >.
$(function() {
$( "#dob" ).datepicker({
changeMonth: true,
changeYear: true,
yearRange: '1900:'
});
$( "#dob" ).datepicker( "option", "dateFormat", "dd-mm-yy" );
});
</script>
</head>
<body>
<form>
<input type="text" name="dob" id="dob" value="<?php if(isset($_SESSION['dob'])) echo $_SESSION['dob']; ?>" placeholder="Please enter date of birth" required/>
</form>
<body>
</html>
There can be many reasons for this issue.
Either the dob variable is null/empty/undefined.
The dob value is not in the correct format.
You have not used defaultDate in the datepicker.
For 1st part, you can only log and debug what is wrong.
For 2nd part, you can format the value in correct format. Search on SO for formatting a string to Date.
For the 3rd part:
$(function () {
$("#dob").datepicker({
defaultDate: $(this).val(), //Add this
dateFormat: 'dd-mm-yy', // And this too
changeMonth: true,
changeYear: true,
yearRange: '1900:'
});
});

Jquery-ui datepicker php+mysql issue

When i set my input field without datepicker like that:
<input type="text" name="date" value="<?php echo $date; ?>" class="textbox" />
I can see my value which i get from database. The format is: 1965-02-05
When I set this code:
<script>
$(function() {
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true
}
);
$( "#datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
$( "#datepicker" ).datepicker( "option", "yearRange", '1900:2011' );
});
</script>
<input type="text" id="datepicker" name="date" value="<?php echo $date; ?>" class="textbox" />
nothing shown in text input area, but i can see datepicker without any problem when i click inside of the text input area.
What is the problem?
I'am formatting like that:
http://docs.jquery.com/UI/Datepicker/formatDate
$( "#datepicker" ).datepicker({
changeMonth: true,
changeYear: true,
dateFormat: 'yy-mm-dd',
yearRange: '1900:2011',
constrainInput: false
});
See working example: http://jsfiddle.net/eATRv/
Try this. Now you should also be able to see if it is the correct format :)

jquery datepicker returning a double year i.e. 05-01-20102010

$(function() {
$(".datepicker").datepicker({
dateFormat: 'mm-dd-yy',
yearRange: 'c-70:c+10',
changeYear: true,
changeMonth: true,
numberOfMonths: 3,
showWeek: true,
firstDay: 1,
showOn: 'button',
buttonImage: "/images/scw.gif"; ?>',
buttonImageOnly: true
});
});
<input type='text' name='from_date' id='from_date' class='datepicker'>
<input type='text' name='to_date' id='to_date' class='datepicker'>
I was able to reproduce this with a recent version of jQuery with a date format of
{
dateFormat: 'mm-dd-yyyy'
}
Consider yy to be a full 4 digits year.
Edit: Is this the only instance of .datepicker invocation in your script? Are there any calls? Is this using a custom locale thats not en? Do you have a link?
Are you sure its not yyyy instead of yy?
I had the same issue, it was because I called jQuery().datepicker() twice.

Categories