为什么我的javascript无法工作?

为什么我的javascript无法工作?,javascript,php,if-statement,Javascript,Php,If Statement,我已经研究这个代码有一段时间了。其思想是让javascript根据HTML表单的结果(不在此页面上)使div可见。然而,我的javascript函数从来都不起作用。我已经将问题隔离到了一开始没有调用脚本的地方。这是我的密码: <!DOCTYPE HTML> <html> <body> <?php While ($result=mysql_fetch_array($data)){ //I have mySQL code before this

我已经研究这个代码有一段时间了。其思想是让javascript根据HTML表单的结果(不在此页面上)使div可见。然而,我的javascript函数从来都不起作用。我已经将问题隔离到了一开始没有调用脚本的地方。这是我的密码:

<!DOCTYPE HTML>
<html>
<body>
<?php
While ($result=mysql_fetch_array($data)){ //I have mySQL code before this
        $equipment= $result['safety equipment'];
$equipment2= str_replace(' ', '_', $equipment); //modified equipment name without spaces so that post can read it
            $problem = $_POST[$equipment2];
            ?>
<div style="display:none;" id="<?php echo $equipment?>"> <!--Code that creates a div for each equipment -->
            <h1>Report a problem</h1> <br>
            You reported that there is a problem with the <?php echo $equipment." in ".$name;?>.<br>
            Please describe the problem.
            <form>
                <textarea row="5" column="300" name="Issue">                        
                </textarea>
            </form>
            </div>
            <?php
    if ($problem=="working"){
        inspectRoom($UID,$equipment,null);
    }else {
        echo $equipment; //this part works
        echo'<script type="text/javascript">'; //this part does not work
        echo'console.log("test");';
        echo'var test ='.$equipment.';';
        echo'alert (test);';
        echo'Appear(test);';
        echo'</script>';                    
    }
    }
    ?>
    <script type="text/javascript">
        function Appear(equipment){
            alert("hi"); //error trapping
            document.getElementById(equipment).style.display='block';
        }
        </script>

</body>
</html>

功能显示(设备){
警报(“hi”);//错误捕获
document.getElementById(设备).style.display='block';
}
$equipment是一个带有设备名称的字符串(例如:通风柜) $problem是从上一页检索到的字符串。它也有一些设备的名称。 在我的控制台中,出现以下错误: “SyntaxError:缺少;在语句之前”
我做错了什么

您将基于PHP的文本直接转储到Javascript上下文中,这是非常危险的。任何JS元字符(尤其是
)都会导致语法错误并杀死整个JS代码块

    echo'var test ='.$equipment.';';
应该是

  echo 'var test = ', json_encode($equipment), This will produce syntactically valid JS code, no matter what's in `$equipment`.

另外,还有许多其他语法错误。php
while
不包含在
代码块中,因此它将作为原始文本直接显示在输出中。while循环中的html现在被认为是PHP代码的一部分,因此这将是另一个语法错误。等等等换句话说,这段代码完全被破坏了。

只谈论javascript,因为这就是你的问题所在,您的
出现
函数永远不会被定义,因为PHP代码会在函数定义之前回显调用
出现
的JS。

首先,第6行缺少一个>。

它不起作用,因为您忘记了回显后的空格。还有其他错误,例如您的while不在标记内。

对于这部分代码

    echo $equipment; //this part works
    echo'<script type="text/javascript">'; //this part does not work
    echo'console.log("test");';
    echo'var test ='.$equipment.';';
    echo'alert (test);';
    echo'Appear(test);';
    echo'</script>'; 
echo$设备//这部分有效
回声'//这部分不起作用
echo'console.log(“test”);
echo“var测试=”.$equipment.;”;
回声“警报(测试)”;
回声“出现(测试)”;
回声';
换成

    echo $equipment; //this part works
    echo'<script type="text/javascript">'; //this part does not work
    echo'console.log("test");';
    echo"var test ='$equipment';";
    echo'alert (test);';
    echo'Appear(test);';
    echo'</script>'; 
echo$设备//这部分有效
回声'//这部分不起作用
echo'console.log(“test”);
echo“var测试='$equipment';”;
回声“警报(测试)”;
回声“出现(测试)”;
回声';

当值为字符串时,Th变量不被引号包围

你没有打开$equipment包含什么?有没有可能你应该把它用引号括起来呢?请你分享一下到底是什么输出到浏览器的?我认为在Javascript中回显
$device
可能存在问题。HTML内容中没有PHP代码。请使用IDE编写代码。这将立即显示所有基本语法错误。我按照建议使用了json_编码,但我现在的错误是“未定义显示”。问题是,由于getElementById部分,我需要在定义div之后定义我的函数。我想我可以把函数放在div和调用之间,但我不希望函数在while循环中定义。