0

I am having problems passing any kind of variable from PHP to JavaScript. Here is a simple example at jsFiddle. Why does it not return my string?

http://jsfiddle.net/funinabox/VkNe2/1/

<?php
//create php test string    
$teststring = "mystring"; 
?>


//Convert Php string to JavaScript string
var testvar = <?php echo $teststring; ?> ;
//Display output of the array elements;
alert(testvar);
halfer
  • 19,824
  • 17
  • 99
  • 186
user2301506
  • 290
  • 5
  • 11
  • 2
    you forgot the quotes: `var testvar = "";` – x4rf41 Apr 22 '13 at 13:50
  • 1
    You can't test PHP on jsfiddle. `Uncaught SyntaxError: Unexpected token <` – jbabey Apr 22 '13 at 13:50
  • Look at the generated source code and you will be enlightened. (hopefully) – SLaks Apr 22 '13 at 13:51
  • are you sure the php part comes before the – Aris Apr 22 '13 at 14:48
  • I should think the jsFiddle server doesn't let you process any PHP at all. It's quite a bit security challenge to let you do that, though some services do. – halfer Apr 22 '13 at 15:16
  • @Aris. Yes, I cut+paste the code from DrColossos into a file called testit.html. I then run it as http://localhost/testit.html and I get the popup from the alert but the only thing being displayed is and not the value of the string from the php code. – user2301506 Apr 22 '13 at 20:18
  • If it helps - I was using xampp but I also tested it with uniform server and get exactly the same behavior. If I save the php into a testit.php and run that as localhost/testit.php with an echo in the php code I get the value of the string. Calling php from within javascript just is not working for me. I cannot believe it can be so difficult but I am out of ideas at the moment. – user2301506 Apr 22 '13 at 20:21
  • @user2301506 the file must be named testit.php. Only then it will be able to parse the php code. – Aris Apr 23 '13 at 04:13

4 Answers4

4

You are missing "

var testvar = "<?php echo $teststring; ?>";

Here is a full example

<?php
//create php test string    
$teststring = "mystring"; 
?>


<html>
   <head>
   <script>
   //Convert Php string to JavaScript string
    var testvar = "<?php echo $teststring; ?>" ;
    //Display output of the array elements;
    alert(testvar);
    </script>
    </head>
<body></body>
</html>
DrColossos
  • 12,656
  • 3
  • 46
  • 67
  • Doesn't work. When I put it in quotes what I get back in the alert is the string not mystring. – user2301506 Apr 22 '13 at 14:29
  • @user2301506 I added an example demonstrating this. It does work. Are you sure, you are using ` – DrColossos Apr 22 '13 at 14:33
  • Yes. I copy+pasted your code and all I get in the alert popup is the literal: – user2301506 Apr 22 '13 at 20:15
  • 1
    If you get it literal, then there is no PHP environment running. You cannot run this from a file, you need to put it on a webserver that knows how to process PHP – DrColossos Apr 23 '13 at 07:14
  • I also got literal when trying to shorthand a la " echo $var; ?>" at least when working locally with Xampp – Helto Dec 23 '13 at 21:31
1

I reinstalled xampp and then made 1 change in c:\xampp\apache\conf\httpd.conf in the mime section section by adding (I did it in line 402 but anywhere in that section should be ok)... AddType application/x-httpd-php .html .htm

NOW IT WORKS!!!!!!!! This looks like a big mistake in the current xampp distribution for Win 7 32-bit.

user2301506
  • 290
  • 5
  • 11
  • This is not a mistake in XAMPP: by default, `.html` files are not passed through the PHP engine, as this would slow them down unnecessarily. In general, if you are writing new PHP pages, give them a `.php` suffix instead. – halfer Apr 27 '13 at 11:03
0

try to do link below:

<?php
//create php test string    
$teststring = "mystring"; 
?>


//Convert Php string to JavaScript string
var testvar = '<?php echo $teststring; ?>' ;
//Display output of the array elements;
alert(testvar);
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
  • Doesn't work. When I put it in quotes (single or double) what I get back in the alert is the string not mystring. – user2301506 Apr 22 '13 at 14:30
0

My environment uses templates so this is not copy and paste code. However I was able to pass the variable to Javascript by doing this:

$teststring = 'mystring'; 

 $page_headers = <<<PAGEHEADERS
 <script>
    window.onload=function(){   
        var testvar = '$teststring';
        alert(testvar);
    };
</script>
PAGEHEADERS;

As long as the php variable is defined first you should be able to get a value from it simply by calling it.

Useless Intern
  • 1,294
  • 1
  • 10
  • 20