Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用document.getElementById时未呈现某些Javascript代码_Javascript_Php_Html - Fatal编程技术网

使用document.getElementById时未呈现某些Javascript代码

使用document.getElementById时未呈现某些Javascript代码,javascript,php,html,Javascript,Php,Html,PHP代码 <input type="text" placeholder="ID" name="instructor_id"><br> <div class="errors"> <strong id="errorSeven"></strong> </div> id=errorSeven的html元素确实存在,但即使

PHP代码

<input type="text" placeholder="ID" name="instructor_id"><br>
<div class="errors">
    <strong id="errorSeven"></strong> 
</div>
id=errorSeven的html元素确实存在,但即使if语句为true,也不会发生任何事情

当用户输入数据库中已经存在的id时,javascript中的错误必须打印在id=errorSeven的元素中

相同的代码在许多不同的地方被使用,它工作得很好,但在这里却不行。即使if语句返回true并且select语句的行数等于1。
我在if语句中打印了一条语句,它被打印出来了

打开浏览器的开发者工具控制台,查看那里的错误消息


JS字符串中不能有文字新行。

只有在呈现页面时,脚本才需要执行JS

试试这个:

$queryCheck1 = "SELECT instructor_id FROM instructor WHERE instructor_id = $instructorId";
$queryCheck1 = mysqli_query($conn, $queryCheck1);
$countCheck1 = mysqli_num_rows($queryCheck1);
if ($countCheck1 == 1) {
    ?>      
    <script>
        document.getElementById("errorSeven").innerHTML = "<strong style=\"color:red\">&#9733 
        </strong>Instructor Id is already asscociated with another instructor.";
    </script>
    <?php
}
<script>
// self executing function here
(function() {
   document.getElementById("errorSeven").innerHTML = "<strong style=\"color:red\">&#9733 
        </strong>Instructor Id is already asscociated with another instructor.";
})();
</script>

我试着照你说的做,但没用。正如我之前所说,我在不同的地方使用相同的代码,用于不同的条件,效果很好,如果$u POST['讲师id']为空,则代码如下{?>document.getElementByIderrorSeven.innerHTML=&9733讲师Id是必需的;为什么不直接在PHP中回显errorSeven div中的变量呢?我应该怎么做?我不是动态检查,PHP代码会在用户单击按钮后验证这一点。请不要再问同样的问题。您已经阅读了我今天问了这个问题:
$queryCheck1 = "SELECT instructor_id FROM instructor WHERE instructor_id = $instructorId";
$queryCheck1 = mysqli_query($conn, $queryCheck1);
$countCheck1 = mysqli_num_rows($queryCheck1);
if ($countCheck1 == 1) {
    ?>      
    <script>
        document.getElementById("errorSeven").innerHTML = "<strong style=\"color:red\">&#9733 
        </strong>Instructor Id is already asscociated with another instructor.";
    </script>
    <?php
}
<script>
// self executing function here
(function() {
   document.getElementById("errorSeven").innerHTML = "<strong style=\"color:red\">&#9733 
        </strong>Instructor Id is already asscociated with another instructor.";
})();
</script>