簡體   English   中英

JavaScript函數無法在php代碼中調用

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

我的function error()無法從php代碼調用。 所以請幫我解決這個問題。 在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>

將該PHP塊放在嘗試錯誤的 div元素之后。 您正在調用error()函數,該函數甚至在瀏覽器中創建該元素之前就對其進行操作。

正如注釋中所建議的那樣,不要將PHP放入<div id="wrong-attempt"></div>元素中,而不必打印不必要的JavaScript函數,而只是簡單地打印出消息,或整個元素,即使沒有錯誤也不會發送此div

並將JavaScript放在頁腳的</body>標記之前,如注釋中所建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM