1

HTML/PHP:

<button onclick="change_value(<?php echo $date?>)">Go</button>

JavaScript:

function change_value(date_used)
{
    alert(date_used);
}

When I right-click on the button and click the "Inspect Element" button shows the parameter correctly, i.e something like this:

<button onclick="change_value(2012-08-22)">Go</button>

But the alert in the JavaScript is displaying 1981. That's it - only 1981. Not only the date is wrong, but the format, too.

What is this happening and how can I fix it?

3
  • 4
    Because you passed an expression (2012 - 08 - 22) instead of a string ("2012-08-22") into change_value. It's like asking "Why does print(3-2); output 1?". Use change_value(<?php echo json_encode($date); ?>) instead. Commented Sep 22, 2012 at 10:54
  • yes , exactly , and you may use a ' instead of json_encode , change_value('<?php echo $date; ?>') Commented Sep 22, 2012 at 10:59
  • that's because 2012-08-22 = 2012 - 1 - 22 = 1981. Your dat is interpreted as a numeric operation and 08 is interpreted as an octal number (08 = 1 in octal) Commented Sep 22, 2012 at 11:01

1 Answer 1

4

Change

<button onclick="change_value(<?php echo $date?>)">Go</button>

to this:

<button onclick="change_value('<?php echo $date?>')">Go</button>

You should pass it as string to JavaScript ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.