简体   繁体   English

JavaScript函数无法在php代码中调用

[英]JavaScript Function not working on call within php code

My function error() is not working on calling from php code. 我的function error()无法从php代码调用。 so Kindly help me to resolve this problem. 所以请帮我解决这个问题。 fucntion works properly when called outside the php code.but no output shown when called insisde the php code. 在php代码之外调用fucntion可以正常工作。但是在坚持php代码时未显示任何输出。

<html>
<head>
    <title>Login Page</title>
    <link rel="stylesheet" href="css/design.css">
    <script type="text/JavaScript">
        function error(){
        document.getElementById("wrong-attempt").innerHTML="Wrong Username Or Password";
        }
    </script>
    <?php
        if(isset($_POST['submit'])){
            echo '<script> error(); </script>';
        }
    ?>
</head>
<body background="pics/background.jpg">
<div class="wrapper">
<center>
    <div class="form-wrapper">
        <form action="index.php" method="POST">
            <div>
            </div>
                <h1>User Login</h1>
            <div>
                <img src="pics/login.png" width="100" height="100"/>
            </div>
            <div>
                <input name="ID" placeholder=" Username" type="text" required="true"/></br>
                <input name="pass" placeholder=" Password" type="password" required="true"></br>
                <input type="image" src="pics/loginb.png" name="submit" value="Login" width="100"/>
            </div>
        </form>
            <div id="wrong-attempt">
            </div>
    </div>
<center/>
</div>
</body>'
</html>

Put that PHP block after the wrong-attempt div element. 将该PHP块放在尝试错误的 div元素之后。 You are calling the error() function which manipulates that element before it is even created in the browser. 您正在调用error()函数,该函数甚至在浏览器中创建该元素之前就对其进行操作。

As it has also been suggested in the comments, instead of complicating with an unnecessary JavaScript function call, just place that PHP if into the <div id="wrong-attempt"></div> element, and simply print out the message, or the whole element, to not even send this div if there is no error. 正如注释中所建议的那样,不要将PHP放入<div id="wrong-attempt"></div>元素中,而不必打印不必要的JavaScript函数,而只是简单地打印出消息,或整个元素,即使没有错误也不会发送此div

And put your JavaScript into the footer, just before the closing </body> tag, as also suggested in comments. 并将JavaScript放在页脚的</body>标记之前,如注释中所建议。

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

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