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);
8
  • 2
    you forgot the quotes: var testvar = "<?php echo $teststring; ?>"; Commented Apr 22, 2013 at 13:50
  • 1
    You can't test PHP on jsfiddle. Uncaught SyntaxError: Unexpected token < Commented Apr 22, 2013 at 13:50
  • Look at the generated source code and you will be enlightened. (hopefully) Commented Apr 22, 2013 at 13:51
  • are you sure the php part comes before the <script> part? Commented Apr 22, 2013 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. Commented Apr 22, 2013 at 15:16

4 Answers 4

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>
Sign up to request clarification or add additional context in comments.

5 Comments

Doesn't work. When I put it in quotes what I get back in the alert is the string <?php echo $teststring; ?> not mystring.
@user2301506 I added an example demonstrating this. It does work. Are you sure, you are using <script> tags?
Yes. I copy+pasted your code and all I get in the alert popup is the literal: <?php echo $teststring; ?>
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
I also got literal when trying to shorthand a la "<? echo $var; ?>" at least when working locally with Xampp
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.

1 Comment

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.
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);

1 Comment

Doesn't work. When I put it in quotes (single or double) what I get back in the alert is the string <?php echo $teststring; ?> not mystring.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.