Reputation: 814
I have a php date and wish to echo it out in a javascript alert box:-
$day=15;
$month=8;
$year=2012;
$date_display = date("Y-m-d", mktime(0, 0, 0, $month, $day, $year));
echo $date_display; // 2012-08-15
Then,
<a href="#" onclick="give_date(<?=$date_display;?>)"><?=$day;?></a>
The javascript function:
<script>
function give_date(value){
alert (value);
}
</script>
Interestingly, the alert box give me "1989", which equals to 2012 minus 8 minus 15!! what shall I do!!
Upvotes: 3
Views: 774
Reputation: 7341
You are making the client-side code show an alert
with the date of the server when the page was loaded.
If you want to show the user the current time, use this:
HTML:
<a href="#" id="date">Please enable JavaScript.</a>
JavaScript:
<script type="text/javascript">
function show_date() {
alert(new Date());
}
window.onload = function() {
document.getElementById('date').innerHTML = new Date().getDay();
};
document.getElementById('date').onclick = show_date;
</script>
Upvotes: 1
Reputation: 30453
Now you get: <a href="#" onclick="give_date(2012-08-15)">15</a>
, so it calculates it in browser.
the solution is simple - add quotes:
<a href="#" onclick="give_date('<?=$date_display;?>')"><?=$day;?></a>
Then you get: <a href="#" onclick="give_date('2012-08-15')">15</a>
Upvotes: 3
Reputation: 9823
You can use jQuery if you are up for it
<a href="#" id="<?php echo $date_display; ?>">Click to get date popup</a>
$(function(){
$('a').click(function(){
var dt = $(this).attr('id');
alert(dt);
});
});
Upvotes: 0