954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Date range picker using PHP&Javascript calendar

Hi guys. I'm trying to pick 2 dates from a calendar on my webpage. I've got 2 textboxes (date1 and date2) and the calendar image next to each of them which opens a separate window with the calendar. It's all working fine, except for the fact that I can not seem to get the second date, even though I'm choosing it from the calendar. I'm not very skilled in Javascript, but I think the problem is within the insertdate() function and I don't know hoe to fix it. I appreciate any help. Here's the relevant pieces of code: [CODE] ]//mypage.php ... ... echo""; echo"\"Pick"; echo "
"; echo""; echo"\"Pick"; ... //scripts.js function viewcalendar() { kalendarik = window.open("php_calendar/calendar.php", "kalendarik" , "location=0, menubar=0, scrollbars=0, status=0, titlebar=0, toolbar=0, directories=0, resizable=1, width=200, height=240, top=50, left=250"); kalendarik.resizeTo(200, 240); kalendarik.moveTo(250, 50); }

function insertdate(d) {
window.close();
window.opener.document.getElementById('date').value = d;

}

//calendar.php
include 'configure.php';
$month = isset($_GET['month'])? $_GET['month'] : date('n');
$pd = mktime (0,0,0,$month,1,date('Y'));// timestamp of the first day
$zd = -(date('w', $pd)? (date('w', $pd)-1) : 6)+1;// monday before
$kd = date('t', $pd);// last day of moth
echo '

«
'.$month_names[date('n', mktime(0,0,0,$month,1,date('Y')))].' '.date('Y', mktime(0,0,0,$month,1,date('Y'))).'
»

';
for ($d=0;$d<7;$d++) {
echo '
'.$day_names[$d].'';
}
echo '
';
for ($d=$zd;$d<=$kd;$d++) {
$i = mktime (0,0,0,$month,$d,date('Y'));
if ($i >= $pd) {
$today = (date('Ymd') == date('Ymd', $i))? '_today' : '';
$minulost = (date('Ymd') >= date('Ymd', $i+86400)) && !$allow_past;
echo '
'.($minulost? date('j', $i) : ''.date('j', $i).'').'';

} else {
echo '
 ';
}
if (date('w', $i) == 0 && $i >= $pd) {
echo '
';
}
}
[/CODE

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

I've still didn't figure it out, so any suggestion would be helpful... thank you.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 
function insertdate(d) {
window.close();
window.opener.document.getElementById('date').value = d;

You are right about this being the issue.
This code is only going to fill the value of a single date element.
If you have two date elements, then it has to be an array.

JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 
function insertdate(d) {
window.close();
window.opener.document.getElementById('date').value = d;

You are right about this being the issue. This code is only going to fill the value of a single date element. If you have two date elements, then it has to be an array.

So you're saying I should have something like that:

function insertdate(d) {
window.close();
window.opener.document.getElementById('date1').value = d[0];
window.close();
window.opener.document.getElementById('date2').value = d[1];
}

But then how do I adapt the PHP code which sets the 2 dates and calls the insertdate function?
michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

I don't know if both dates will be there or not. It depends on how that part of the code is.
Maybe the simplest approach is to do one at a time, but they STILL need to be differentiated from each other somehow, like date1 date2 or date[].
If the output of the calender is d , call it for each field and drop it into value.
Those element exist, (via hard coding ,PHP echo, or js DOM.) so when the value is populated by d PHP will pick them up as
$_POST[date1], $_POST[date2]

function insertdate(d) {
window.close();
window.opener.document.getElementById('date1').value = d[0];
window.close();
window.opener.document.getElementById('date2').value = d[1];
}
JRM
Practically a Master Poster
621 posts since Nov 2006
Reputation Points: 130
Solved Threads: 75
 

I did not manage to make it work, so I replaced the calendars with drop down lists for dates which work just fine.

michelleradu
Junior Poster in Training
55 posts since Dec 2009
Reputation Points: 10
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You