0

I would like to echo some php code in javascript for certain reasons but the code does not get executed, when i use the code inspector i notice that the php code has been commented out.

This is the code:

var code = '<?php echo time_passed(strtotime(' + new Date().toLocaleString() + ')); ?>';
$('#page').html(code);

basically i am passing the javascript datetime value to a php function which should then echo the results into the code variable.

I then output the value of code to a container with an id of page.

user3718908x100
  • 7,939
  • 15
  • 64
  • 123
  • [How to pass JavaScript variables to PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) Client-side JavaScript and server-side PHP can't communicate directly with each other. They require an HTTP request to pass messages, including data such as the current locale date string, as by [Ajax](https://developer.mozilla.org/en-US/docs/AJAX). – Jonathan Lonowski Jun 08 '14 at 03:28

1 Answers1

0

PHP is on the server end. In the manner your using, there is no way to for PHP and JavaScript code execution to interact. You would need to user AJAX to communicate between front end execution (JavaScript) and back end execution (PHP).