javascript中的PHP变量

javascript中的PHP变量,javascript,php,Javascript,Php,我试图用下面的方式在javascript中使用php变量,但它不起作用。 有人能告诉我下面的代码有什么问题吗 include './connection.php'; $rating[]=$_GET['rating']; echo '<script>'; echo 'document.getElementById(' .$rating. ').checked = true;'; echo '</script>'; include'/connection.php'; $r

我试图用下面的方式在javascript中使用php变量,但它不起作用。 有人能告诉我下面的代码有什么问题吗

include './connection.php';
$rating[]=$_GET['rating'];

echo '<script>';
echo 'document.getElementById(' .$rating. ').checked = true;';
echo '</script>';
include'/connection.php';
$rating[]=$\u获得['rating'];
回声';
echo'document.getElementById('.$rating.')。checked=true;';
回声';

我猜它应该是一个字符串,如果是的话,你必须引用它

include './connection.php';
$rating = $_GET['rating'];

echo '<script>';
echo 'document.getElementById("' .$rating. '").checked = true;';
echo '</script>'; //          ^^            ^^
include'/connection.php';
$rating=$_获得['rating'];
回声';
echo'document.getElementById(“.$rating.”)。checked=true;';
回显“”;/^^^

我猜它应该是一个字符串,如果是的话,你必须引用它

include './connection.php';
$rating = $_GET['rating'];

echo '<script>';
echo 'document.getElementById("' .$rating. '").checked = true;';
echo '</script>'; //          ^^            ^^
include'/connection.php';
$rating=$_获得['rating'];
回声';
echo'document.getElementById(“.$rating.”)。checked=true;';
回显“”;/^^^

您试图回显数组,而不是数组中的值

为什么将
$rating
定义为数组?只需这样做:

include './connection.php';
$rating=$_GET['rating'];
?>
<script>
document.getElementById('<?php echo $rating; ?>').checked = true;
</script>
<?php
// continue script
include'/connection.php';
$rating=$_获得['rating'];
?>
document.getElementById(“”).checked=true;

您正在尝试回显数组,而不是数组中的值

为什么将
$rating
定义为数组?只需这样做:

include './connection.php';
$rating=$_GET['rating'];
?>
<script>
document.getElementById('<?php echo $rating; ?>').checked = true;
</script>
<?php
// continue script
include'/connection.php';
$rating=$_获得['rating'];
?>
document.getElementById(“”).checked=true;
include'/connection.php';
$rating=$_获得['rating'];//忘记方括号吧,这里不需要数组
回声';
echo'document.getElementById(“.htmlspecialchars($rating)。”)”。checked=true;
回声';
确保您的网站不易受到攻击。 document.getElementById接受一个字符串参数,因此您需要将其用引号括起来。

include./connection.php';
$rating=$_获得['rating'];//忘记方括号吧,这里不需要数组
回声';
echo'document.getElementById(“.htmlspecialchars($rating)。”)”。checked=true;
回声';
确保您的网站不易受到攻击。
document.getElementById接受一个字符串参数,因此您需要将其用引号括起来。

如果启用错误报告,您应该会收到一条消息,告诉您发生了什么。与检查呈现的htmlRemove第二行$rating中的括号时相同。请尝试执行
echo'document.getElementById(“.$rating.”)。checked=true如果启用错误报告,您应该会收到一条消息,告诉您发生了什么。与检查呈现的htmlRemove第二行$rating中的括号时相同。请尝试执行
echo'document.getElementById(“.$rating.”)。checked=true如果我们在优化,为什么还要设置
$rating
?只要做
@Wogan就行了是的。我们可以这样做,但实际上,
$rating
应该在回显之前进行消毒。如果我们在优化,为什么还要设置
$rating
?只要做
@Wogan就行了是的。可以这样做,但实际上,
$rating
应该在回显之前进行消毒。可能应该链接到英文维基百科;)可能应该链接到英文维基百科;)