It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

2

It's not possible. Send the values by using Ajax. Or mount the javascript code by using PHP.
–
The MaskMay 10 '12 at 21:58

1

PHP is executed on the server JavaScript is executed on the client (with a few exceptions such as node.js).
–
Jack ManeyMay 10 '12 at 21:58

PHP is ran on the server, and its output is sent to your browser, where the JavaScript is ran.
–
Rocket HazmatMay 10 '12 at 21:59

You can echo it into a JS script. In your example, you are echoing $cat so that would be evaluated by the PHP engine and that would be in your JS as if it were plain text on page load. However, you may be wanting JSON.Stringify() which is a javascript function to create a JSON object.
–
BlakeMay 10 '12 at 22:02

2 Answers
2

PHP executes server side before the client gets the page content. Javascript runs on the client side. To get javascript data to php you need to send another request to the server from javascript. If you want to do this with page reloads, just add the data to the url. Or make an invisible form and submit it.

To do this without page reloads, you will need to setup some sort of API endpoint for your ajax requests to send/receive data to/from.

If you must have php code in your javascript, place your javascript in a .php file.

<script type="text/javascript" src="myjsfile.php"></script>

Please note, however, that the php can't get data from the javascript without an ajax request. ALL of the php throughout the entire file gets parsed and executed, then the resulting javascript is sent to the client.