My function error() is not working on calling from php code.
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.
<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>
1 Answer 1
Put that PHP block after the wrong-attempt div element. You are calling the error() function which manipulates that element before it is even created in the browser.
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.
And put your JavaScript into the footer, just before the closing </body> tag, as also suggested in comments.
1 Comment
</body>
<div id="wrong-attempt"></div>with<?php if( !empty($_POST) ) {echo '<div id="wrong-attempt">Wrong Username Or Password</div>';} ?>and remove the javascript and php in the head. NOTE javascript in head will slow down your page load. In this case it's not much to load but if it's more it will slow downname="submit"if you ever plan on submitting using script