After ajax call, date picker work but the formatting not - php

Below is the Ajax call in index.php
$(document).ready(function() {
$(document).on("click", ".result p", function(e) {
$(this).parent(".result").empty();
var $txt = $(this).text();
document.getElementById("pd").style.display = "block";
document.getElementById("listpd").style.display = "none";
e.preventDefault();
$.ajax({
type: "POST",
url: "piutang.php",
data: {"nama": $txt},
success: function(msg) {
$("#pd").html(msg)
$("#formNama").val('');
},
error: function() {
alert("failure");
}
});
});
});
The piutang.php (inside the Ajax call) has the text box for the date-picker and also the script for the date picker.
<input type="text" id="datepicker" name="tgl" style="width: 85px;" readonly />
<script>
$(function() {
$("#datepicker").datepicker();
});
$('#datepicker').datepicker({
dateFormat: "dd M yy"
}).datepicker("setDate", new Date());
</script>
After the call, inside the div with id:#pd,
I can see the text box of the date picker with the current date.
But it show the default format mm/dd/yyyy not dd M yy.
What I've tried so far is cut the script code in piutang.php,
then paste/insert the code inside Ajax success, right after $("#pd").html(msg),
but the result is the same thing, inside the div with id:#pd,
the text box show the current date with its default format mm/dd/yyyy.
How do I solve this ?
Thank you.

You need to initialize date picker one time with all settings:
$('#datepicker').datepicker({
dateFormat: "dd M yy"
}).datepicker("setDate", new Date());
This one is enough. Remove other one.

Related

how to add datepicker functionality to dynamically editable table column

I have code snipe that works great the problem started when i had to add datepicker on top of it.
working sample:
<td
id="test_<?php echo $id; ?>"
title="Double click to edit"
class="editable"
name="ExpiryDate_<?php echo $id; ?>"
>
<?php echo $Expiry_Date; ?>
</td>
<script>
$(document).ready(function () {
$(".editable").dblclick(function () {
var OriginalContent = $(this).text();
var pieces = $(this).attr("name");
ID= pieces;
var count = 1;
$(this).addClass("cellEditing");
count += 1;
$(this).html("<input id='t1_"+count+"'placeholder='Click to open calendar' type='text' value='' />");
$(this).children().first().focus();
$(this).children().first().keypress(function (e) {
if (e.which == 13) {
var option = confirm('Are you sure u wanna change');
if(option){
..
//Call the server side file to communicate with database.
//send do some staff mostly send GET xmlhttp.send();
..
}
});
$(this).children().first().blur(function(){
$(this).parent().text(OriginalContent);
$(this).parent().removeClass("cellEditing");
});
});
</script>
to add datepicker i add under:$(this).children().first().focus();
var origFocus = document.activeElement;
$("input[id *='t1_']").on('click',function() {
$(this).datepicker({
dateFormat: "dd-mm-yy"
}).focus();
}).blur(function(){
origFocus.focus();
});
and
This datepicker works great I can get dapicker to show dynamically, click on date without editable field closing on me and on datepick widget get close focus return to editable field and i can click Enter key==13 so I can send date to server using GET, problem is when I click anywhere on page widget just reopens I tried a lot of tweaks to make it go away but I really don't know what to do i'm stuck.
What I would like to mention the site use bootstrap v3.1.1 and jquery 1.11.1
Fiddle
Added fiddle, if I remove
$(this).children().first().blur(function(){
$(this).parent().text(OriginalContent);
$(this).parent().removeClass("cellEditing");
});
Then I can't leave editable field but if I leave that part of code I can't make datepick to work. After picking date it should be focus on editable field again so user can click enter to send GET
Maybe you should provide some fiddle, but from what I can see the wrong line of code is:
var origFocus = document.activeElement;
$("input[id *='t1_']").on('click',function() {
$(this).datepicker({
dateFormat: "dd-mm-yy"
}).focus();
}).blur(function(){
origFocus.focus();
});
You shouldn't set focus on origFocus variable, because doing that you re-apply focus on the element. Just try these options:
$("input[id *='t1_']").on('click',function() {
$(this).datepicker({
dateFormat: "dd-mm-yy"
}).focus();
}).blur(function(){
document.activeElement.focus();
});
or
$("input[id *='t1_']").on('click',function() {
$(this).datepicker({
dateFormat: "dd-mm-yy"
}).focus();
}).blur(function(){
document.activeElement = null;
});
or just not setting the blur handler at all.
EDIT
Ok, i cleaned it up a little. Hope it helps
jsfiddle

date range picker default value

Hi all I'm using daterangepicker.jQuery.js
( http://trac.vtiger.com/cgi-bin/trac.cgi/browser/vtigercrm/branches/6.0.0/vtiger6/libraries/jquery/jquery-ui/third-party/jQuery-UI-Date-Range-Picker/js/daterangepicker.jQuery.js?rev=13838 )
per default it's initialized at 1970-01-01 and i've searched in the whole javascript but i didn't find it does someone know how to change this value ? and if possible how to limit the calendar in today's date please
( EDIT )
I've tried
<script type="text/javascript">
$(function(){
$('#rangeBa, #rangeBb').daterangepicker();
});
$(document).ready(function(){
var date = '17-05-2013';
$('#rangeBa, #rangeBb').daterangepicker('setDate', date);?
})
</script>
but it's not working
This works like a charm. You can try it.
var today = new Date();
var endDate = new Date();
endDate.setMonth(endDate.getMonth() + 1);
$('#rangeBa, #rangeBb').daterangepicker({
startDate: today, // after open picker you'll see this dates as picked
endDate: endDate,
locale: {
format: 'DD-MM-YYYY',
}
}, function (start, end, label) {
//what to do after change
}).val(today + " - " + endDate); //set it to see it inside input (don't forget to format dates)
If you want to limit date range just use minDate and maxDate options.
Try like
$(document).ready(function(){
var date = '17-05-2013';
$("#my_input").datepicker('setDate', date);​
})
Make sure that the datepicker date format would be like 'dd-mm-yyyy' only
As per your edit
/*$(function(){
$('#rangeBa, #rangeBb').daterangepicker();
});*/
$(document).ready(function(){
var date = '17-05-2013';
$('#rangeBa, #rangeBb').daterangepicker();
$('#rangeBa, #rangeBb').daterangepicker('setDate', date);
})
and you can try by date object like
defaultDate = new Date(2013,05,17);
$('#rangeBa, #rangeBb').daterangepicker('setDate', defaultDate);
try it.
<script type="text/javascript">
var today = new Date();
$(function () {
$('#rangeBa, #rangeBb').daterangepicker({
"startDate": today,
},
locale: {
format: 'DD-MM-YYYY',
},
function (start) {
startdate = start.format('DD-MM-YYYY')
});
});
</script>
You can try like this and daterangepicker take default date of today..
<input type="text" name="daterange" value="01/01/2018 - 01/15/2018" />
<script>
$(function() {
$('input[name="daterange"]').daterangepicker({
opens: 'left'
}, function(start, end, label) {
console.log("A new date selection was made: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
});
});
</script>

Retrieving and Comparing Dates using JQuery And PHP

I am using jQuery's datepicker in wordpress/PHP and I am using this code to show dates:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function() {
$("#datepicker").datepicker();
$("#datepicker").datepicker("option","dateFormat",'yy-mm-dd');
});
</script>
Date<input type="text" id="datepicker" size="20" maxlength="15" style="width: 80px;" readonly="readonly"/>
It successfully displays my date.
Also, I have a table (wp_redeem_vouchers) in the database that has specific voucher redemption dates saved in it:
id wp_deal_id redeem_date
1 Amazon6767 2012-03-21
2 Ebay87nm 2013-06-12
3 M&Svouc 2013-01-01
4 ASDA 2011-09-08
Question:
How can I retrieve "redeem_date" and compare with the datepicker's textbox date (upon selection) with the date already saved in the database?
Is there anyway I could disable past date so that user can't choose the past date from date picker? i.e The user on 14/03/2013 can't choose 22/02/2013 from datepicker.
For your i)
$('#datepicker').datepicker( {
minDate: 0,
onSelect: function(dateText, inst) {
$.ajax({
type: "POST",
url: "some.php",
data: { date: dateText },
onSuccess: function(e){
alert("success");
}
});
});
Then in the some.php file you do your compare with mysql and you return a success or failure getting the alert(success).
Then I would start with
$date = $_POST['date'];
die($date);
In the some.php just to see if it is passed correctly.
Well, you can pass your datepicker date string (adjust format to $("#datepicker").datepicker("option","dateFormat",'yyyy-mm-dd');) to following JavaScript function which creates a Date object:
function createDate(s) {
var bits = s.split('-');
var d = new Date(bits[0], bits[1] - 1, bits[2]);
return d;
}
Then you have to retrieve the date value from your database.
I do not know if there is a wordpress limitation but you can trigger a Ajax-Request which reads out the database value and give you as a response the date string as a JavaScript variable.
This variable can also be passed to the createDate(date) function. With these two date objects you can call easily compare the date with the time in milliseconds -> getTime() function of date object.
Regards

jquery datepicker manipulation

Can someone please tell me how I can make the datepicker pick a date that is after a certain date I choose.
For example I want the date picker to pick dates after 03/08/2012.
Here is my simple datepicker:
<script type="text/javascript">
$(function(){
$("#date").datepicker({ dateFormat: 'm/d/yy', minDate: 0});
});
</script>
What if the date was stored in a php variable. for eg. $var='03/08/2012'. Is splitting it one by one only way to do it?
I am trying to get the date from a dynamic php variable. For eg:
<script type="text/javascript">
$(function() {
$( "#date" ).datepicker({ minDate: new Date(<?php echo $split_date[2];?>, <?php echo ($split_date[0] - 1);?>, <?php echo $split_date[1]; ?>)});
});
</script>
<?php
$date='03/08/2011';
$split_date=explode("/",$date);
echo $split_date[0];
echo $split_date[1];
echo $split_date[2];
?>
but nothing i tried so far works!
According to jQuery's doc on the Datepicker, you should do
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: -20, maxDate: "+1M +10D" });
});
</script>
Restrict the range of selectable dates with the minDate and maxDate options. Set the beginning and end dates as actual dates (new Date(2009, 1 - 1, 26)), as a numeric offset from today (-20), or as a string of periods and units ('+1M +10D'). For the last, use 'D' for days, 'W' for weeks, 'M' for months, or 'Y' for years.
If you want it to start from 03/08/2012, you should write
<script>
$(function() {
$( "#datepicker" ).datepicker({ minDate: new Date(2012, 3 - 1, 8)});
});
</script>
You can also refer to this answer:
changing minDate option in JQuery DatePicker not working
Try:
<script type="text/javascript">
$(function(){
$("#date").datepicker({
'dateFormat': 'm/d/yy',
'minDate': new Date(<?php echo date('Y, m, d'); ?>)
});
});
</script>

Get jQuery UI Datepicker selected date to a PHP Session variable?

How to get jQuery UI Datepicker selected date to a PHP Session variable when a date selected by user?
Make an AJAX call when the onSelect is trigged.. Just dummy code you can play with:
$('.selector').datepicker({
onSelect: function(dateText, inst) {
$.ajax({
url: "myPHPscript.php?selecteddate=" + dateText,
success: function(){
$(this).addClass("done");
}
});
}
});
Description
You can do this using Ajax. After the Datepicker is closed you can set the Session Variable using a Ajax call.
You can use the DatePicker event onClose if you want to set the variable after the dialog is closed or onSelect after a date is selected. I would prefer the onClose event.
Sample
jQuery
$('.selector').datepicker({
onClose: function(dateText, inst) {
$.post("/backend.php", {"VariableName": dateText});
}
});
PHP (backend.php)
<?php
// set the variable
$_SESSION['VariableName'] = $_POST['VariableName'];
?>
More Information
jQuery.ajax

Categories