Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,554
    Thanks
    80
    Thanked 4,620 Times in 4,583 Posts
    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.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,460
    Thanks
    11
    Thanked 600 Times in 580 Posts
    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...')) {
    Last edited by rnd me; 03-20-2012 at 02:15 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •