1

Possible Duplicate:
Parse a JavaScript file through PHP

I've been trying to pass a php variable to jQuery , but with not much success. Here's the part of the code which I used to pass the data :

 $('#submitAuto').click(function(){
          var isDelete = $("#autoDelete").val();
          var userID = "<?php echo $userData ; ?>" ;

But I find out the userID value is the string, <?php echo $userData ; ?>, instead of $userData value.

How could I handle this problem?

Any suggestion will be welcomed, Thanks in advance

Community
  • 1
  • 1
Itamar
  • 524
  • 1
  • 9
  • 21

3 Answers3

3

Move the JavaScript to within <script> tags and place in a PHP file.

Your server wont run PHP unless it has a .php file extension.

It might also be a good idea to use json_encode() like this:

var userID = <?php echo json_encode($userData); ?>;

This stops any issues with funny characters in your string.

472084
  • 17,666
  • 10
  • 63
  • 81
  • +1 - this solution is covered here http://stackoverflow.com/a/9137093/201648 and http://commacommacrash.com/2009/02/passing-variables-between-javascript.html. Also see the notes here about global scope in JavaScript (the order of the declarations is important) - http://stackoverflow.com/questions/2932782/global-variables-in-javascript-across-multiple-files – Aaron Newton Aug 14 '12 at 09:08
0

Remove "" from the line

var userID = <?php echo $userData; ?>

You should not be picking up php code in js, you should put the value of userData in html element on the page and then you can read it, by using document.getElementByID etc.

You can have a hidden field take the value of userdata and then you can read userdata variable wid the help of javascript.

sushil bharwani
  • 29,685
  • 30
  • 94
  • 128
  • I put the code in a js file , removing " " from the line lead to an error – Itamar Aug 14 '12 at 08:59
  • If you are using a js file, you cannot do it, js file cannot take reference of your php variables. – sushil bharwani Aug 14 '12 at 08:59
  • Removing the quotation marks is not a good idea. It will only work, if he passes `true`, `false`, `null` or a digit. It will crash if he passes a string. Also you're missing a semicolon add the end of the line. – insertusernamehere Aug 14 '12 at 09:09
0

Any file that has PHP code in it, will have to be parsed by PHP to turn the code into something else before it's sent from the server to the client, as PHP only runs on the server.

Generally this means that file has to end with .php, and just sticking PHP code in a .js file will not work unless you configure your server to run the .js files thru PHP.

just renaming the .js file to .php will work most of the time, or you can include the javascript in a .php file with script tags, or change your server configuration to also parse .js files. In Apache you can add to .htaccess :

AddType application/x-httpd-php .js
AddHandler x-httpd-php5 .js
<FilesMatch "\.(js|php)$">
SetHandler application/x-httpd-php
</FilesMatch>
adeneo
  • 312,895
  • 29
  • 395
  • 388