Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2012, 05:38 PM   PM User | #1
nn4n4s
New to the CF scene

 
Join Date: Mar 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
nn4n4s is an unknown quantity at this point
Javascript changes date format?

Hello.

I have got this kind of array:
Code:
$days = array('Monday' => '2012-03-19',
                    'Tuesday' => '2012-03-20'
                   );
I'm iterating like this:
Code:
foreach($days as $key => $value){
       echo '<a href="javascipt:confirmDelete('.$value.');">Delete</a>';
}
Script:
Code:
function confirmDelete(day)
{
	if (confirm('Delete?')) {
		window.location.href = 'processDay.php?action=delete&day=' + day ;
	}
}
While i hover over the link with coursor, it show it good: javascript:confirmDelete(2012-03-19), but when I submit the delete confirmation, i get &day=1987. Where is the problem?
nn4n4s is offline   Reply With Quote
Old 03-19-2012, 09:29 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,548
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
2012-03-19 is a subtraction expression.

2012 minus 3 is 2009.

2009 minus 19 is 1990.

So I don't know why you get 1987 instead of 1990.

Now try putting quotes around your date.
Code:
foreach($days as $key => $value){
       echo '<a href="javascipt:confirmDelete(\''.$value.'\');">Delete</a>';
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 03-20-2012, 01:12 AM   PM User | #3
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,552
Thanks: 9
Thanked 479 Times in 462 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
turn a php var into js using json_encode:

Code:
 echo '<a href="javascipt:confirmDelete('.  json_encode($value)  .');">Delete</a>';
oh, and by the way, confirm() is assertive, not inquisitive, so re-word your question to reflect ok/cancel instead of yes/no for best UX:

Code:
if (confirm('Deleting...')) {
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%

Last edited by rnd me; 03-20-2012 at 01:15 AM..
rnd me is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 PM.


Advertisement
Log in to turn off these ads.