My following code run well without php when I put the array values in script variable. But in php it console.log Uncaught SyntaxError: Unexpected identifier
. Please help me to find my mistakes.
当我将数组值放在脚本变量中时,我的以下代码在没有php的情况下运行良好。但是在php中它是console.log Uncaught SyntaxError:意外的标识符。请帮我找出错误。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<?php
$availableName = array("ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell");
$availableName = json_encode($availableName);
echo "<script type=\"text/javascript\">";
echo " $(function() {";
echo " var allTags =" . $availableName;
echo " $( \"#tags\" ).autocomplete({";
echo " source: allTags";
echo " }); }); </script>";
?>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
3 个解决方案
#1
1
The $ should not harm in this case. If I got you right the problem shows up when executing the output in the browser (so a JavaScript error, instead of a PHP error).
在这种情况下,$不应该受到伤害。如果我说得对,那么在浏览器中执行输出时会出现问题(因此出现JavaScript错误,而不是PHP错误)。
You are putting the JavaScript all in one line, so you are required to add a semicolon to the end (after "Haskel"])
您将JavaScript全部放在一行中,因此您需要在末尾添加分号(在“Haskel”之后))
Instead of
echo " var allTags =" . $availableName;
Try this:
echo " var allTags =" . $availableName . ";";
#2
1
I think it is happen because of this $(function()
and $( \"#tags\" )
我认为这是因为$(function()和$(\“#tags \”)
php will read it as variable then you got error.
php会把它读作变量,然后你就会出错。
Example :
$abc = "test";
echo "$abc123"; //test123
to fix it, change from double quote "
to single quote '
解决它,从双引号“改为单引号”
#3
0
I think this may be related to many of the quotes being escaped improperly. Alternatively, I would probably code it as follows, which cuts down on the PHP to javascript translation mess which provides the added benefit of looking more compact, readable, and concise.
我认为这可能与许多报价被错误地转义有关。或者,我可能会按如下方式对其进行编码,从而减少PHP到javascript的翻译混乱,这提供了更加紧凑,可读和简洁的额外好处。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
var allTags = <?php echo json_encode(array("ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell")); ?>;
$( "#tags" ).autocomplete({'source': allTags});
});
</script>
You'll also notice that I placed quotes around the object key in the autocomplete
options object. This may be an antiquated practice with newer browsers; however, older browsers needed object keys to be quoted in order to parse the json object properly. I find it to still be a good practice because it helps avoid confusing keys with variables at quick glances.
您还会注意到我在自动完成选项对象中的对象键周围放置了引号。对于较新的浏览器,这可能是一种过时的做法;但是,较旧的浏览器需要引用对象键才能正确解析json对象。我发现它仍然是一个很好的做法,因为它有助于避免在快速浏览时将键与变量混淆。
#1
1
The $ should not harm in this case. If I got you right the problem shows up when executing the output in the browser (so a JavaScript error, instead of a PHP error).
在这种情况下,$不应该受到伤害。如果我说得对,那么在浏览器中执行输出时会出现问题(因此出现JavaScript错误,而不是PHP错误)。
You are putting the JavaScript all in one line, so you are required to add a semicolon to the end (after "Haskel"])
您将JavaScript全部放在一行中,因此您需要在末尾添加分号(在“Haskel”之后))
Instead of
echo " var allTags =" . $availableName;
Try this:
echo " var allTags =" . $availableName . ";";
#2
1
I think it is happen because of this $(function()
and $( \"#tags\" )
我认为这是因为$(function()和$(\“#tags \”)
php will read it as variable then you got error.
php会把它读作变量,然后你就会出错。
Example :
$abc = "test";
echo "$abc123"; //test123
to fix it, change from double quote "
to single quote '
解决它,从双引号“改为单引号”
#3
0
I think this may be related to many of the quotes being escaped improperly. Alternatively, I would probably code it as follows, which cuts down on the PHP to javascript translation mess which provides the added benefit of looking more compact, readable, and concise.
我认为这可能与许多报价被错误地转义有关。或者,我可能会按如下方式对其进行编码,从而减少PHP到javascript的翻译混乱,这提供了更加紧凑,可读和简洁的额外好处。
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
$(function() {
var allTags = <?php echo json_encode(array("ActionScript","AppleScript","Asp","BASIC","C","C++","Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell")); ?>;
$( "#tags" ).autocomplete({'source': allTags});
});
</script>
You'll also notice that I placed quotes around the object key in the autocomplete
options object. This may be an antiquated practice with newer browsers; however, older browsers needed object keys to be quoted in order to parse the json object properly. I find it to still be a good practice because it helps avoid confusing keys with variables at quick glances.
您还会注意到我在自动完成选项对象中的对象键周围放置了引号。对于较新的浏览器,这可能是一种过时的做法;但是,较旧的浏览器需要引用对象键才能正确解析json对象。我发现它仍然是一个很好的做法,因为它有助于避免在快速浏览时将键与变量混淆。