Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
share|improve this question
    
you are missing link –  Suresh Kamrushi Apr 22 '13 at 13:48
2  
you forgot the quotes: var testvar = "<?php echo $teststring; ?>"; –  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 <script> part? –  Aris Apr 22 '13 at 14:48
show 4 more comments

4 Answers

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>
share|improve this answer
    
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 Apr 22 '13 at 14:29
    
@user2301506 I added an example demonstrating this. It does work. Are you sure, you are using <script> tags? –  DrColossos Apr 22 '13 at 14:33
    
Yes. I copy+pasted your code and all I get in the alert popup is the literal: <?php echo $teststring; ?> –  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 7: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
add comment

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);
share|improve this answer
    
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. –  user2301506 Apr 22 '13 at 14:30
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.