简体   繁体   English

从PHP使用JavaScript更改HTML属性

[英]Change HTML attribute with JavaScript from PHP

I have this two HTML elements which are hidden and should become visible when someones enters wrong credentials. 我有这两个HTML元素,它们是隐藏的,当有人输入错误的凭据时应该可以看到它们。

Username : 用户名

<div class="alert alert-danger login_alert" id="username_alert" role="alert"><span>Your username is incorrect. Please try again.</span></div>

Password : 密码

<div class="alert alert-danger login_alert" id="password_alert" role="alert"><span>Your password is incorrect. Please try again.</span></div>

In php, when a password is wrong, I try this line: 在php中,当密码错误时,我尝试以下行:

echo '<script>console.log("Error: Password incorrect"); document.getElementById(password_alert).style.display = "block";</script>';

This console.log is displayed in the console, but after that I get: 该console.log显示在控制台中,但之后我得到:

Uncaught ReferenceError: password_alert is not defined
    at login.php:15

This while password_alert is defined in my div . 这在我的div定义了password_alert I also tried this using ClassName() by creating two login_alert classes for both username and password, but that isn't working either. 我还使用ClassName()通过为用户名和密码创建两个login_alert类来尝试此操作,但这都不起作用。

Efectively you have to change the 有效地,您必须更改

document.getElementById(password_alert)

For 对于

document.getElementById("password_alert")

From the definition of getElementById Manual 从getElementById 手册的定义

按照@RobinZigmond的建议,通过将整个PHP部分移到HTML <body>的末尾来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM