How can we print a date onclick?
This is a calendar script.
Here when I click on a particular date then it will alert.
But how can we print this date on onclick?
<?php
/* call calendar function */
function fncalendar($month,$year){
/* Outer Table for calendar */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
/* Calendar Days in heading */
$headings = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
/* get and set primary setup from input :: put it in array */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
/* row for single week */
$calendar.= '<tr class="calendar-row">';
for($x = 0; $x < $running_day; $x++) {
$calendar.= '<td class="calendar-day-np"> </td>';
$days_in_this_week++;
}
for($list_day = 1; $list_day <= $days_in_month; $list_day++) {
$calendar.= '<td class="calendar-day">';
$createdDatei = $list_day . '/' . $month . '/' . $year;
$calendar .= '<a id="'.$createdDatei.'" class="done" onclick="alert(this.id);" href="javascript:;"><em id="e'.$createdDatei.'">10</em>' .$list_day. '</a>';
$calendar.= '</td>';
if($running_day == 6) {
$calendar.= '</tr>';
if(($day_counter+1) != $days_in_month) {
$calendar.= '<tr class="calendar-row">';
}
$running_day = -1;
$days_in_this_week = 0;
}
$days_in_this_week++; $running_day++; $day_counter++;
}
if($days_in_this_week < 8) {
for($x = 1; $x <= (8 - $days_in_this_week); $x++) {
$calendar.= '<td class="calendar-day-np"> </td>';
}
}
$calendar.= '</tr>';
$calendar.= '</table>';
/* all done, return final result */
return $calendar;
}
/* sample usages 开发者_开发知识库*/
echo '<h2>July 2011</h2>';
echo fncalendar(7,2011);
echo '<h2>August 2011</h2>';
echo fncalendar(8,2011);
?>
<style type="text/css">
/* Css style applying after creating calendar */
/* main */
body { font-family: Airal, sans-serif; }
table.calendar { border-left:1px solid #ddd; font-family: Arial, sans-serif; }
tr.calendar-row { }
td.calendar-day { position: relative; font-size:11px; }
* html div.calendar-day { height:80px; }
td.calendar-day:hover { background:#eceff5; }
td.calendar-day-np { background:#eee; min-height:80px; }
* html div.calendar-day-np { height:80px; }
td.calendar-day-head { background:#ccc; font-weight:bold; text-align:center; width:120px; padding:5px; border-bottom:1px solid #ddd; border-top:1px solid #ddd; border-right:1px solid #ddd; }
div.day-number { background:#999; padding:0px; color:#fff; font-weight:bold; float:right; margin:-5px -5px 0 0; width:20px; text-align:center; }
/* shared */
td.calendar-day, td.calendar-day-np { width:120px; border-bottom:1px solid #ddd; border-right:1px solid #ddd; }
a { display: block; text-align: right; font-size: 22px; padding: 5px 10px; text-decoration: none; font-weight: bold; color: blue; line-height: 40px; }
a.done { background: #DCFFDB url(http://images.yincsolutions.com/checkgr.png) no-repeat 20% 80%; }
a.current { background: #40a9da; -moz-box-shadow: inset 0 0px 5px rgba(0, 0, 0, 0.7); -webkit-box-shadow: inset 0 0px 5px rgba(0, 0, 0, 0.7); box-shadow: inset 0 0px 5px rgba(0, 0, 0, 0.7); }
a:active { color: red; }
a em { float: left; font-size: 17px; color: #aaa; font-weight: normal; margin: -10px 0px 0px -5px; }
</style>
I think by "print" your mean into a HTML element.
Try creating a placeholder element : <p id="foo">Placeholder</p>
and, using javascript, override its content :
function setText(text) {
document.getElementById("foo").innerHTML = text;
}
In your link code, in place of alert()
, put :
onclick="setText(this.id);"
Also, note that mixing PHP and HTML/CSS is poor practice. I highly recommend separating the logic into different classes, and using a template for displaying the information.
精彩评论