我在将任何类型的变量从 PHP 传递到 JavaScript 时遇到问题。这是 jsFiddle 上的一个简单示例。为什么它不返回我的字符串?
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);
最佳答案
您缺少”
var testvar = "<?php echo $teststring; ?>";
这是一个完整的示例
<?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>
关于php - 将 PHP 变量传递给 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16148772/