简体   繁体   中英

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.

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:

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:

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

This while password_alert is defined in my div . I also tried this using ClassName() by creating two login_alert classes for both username and password, but that isn't working either.

Efectively you have to change the

document.getElementById(password_alert)

For

document.getElementById("password_alert")

From the definition of getElementById Manual

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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