You can put your php code anywhere as you would like, say in the body as an attribute. You can try the following code:
<body <?php if($_SERVER['REQUEST_METHOD'] == 'POST') { if($_POST['txtUsername']=='') { echo "onload = 'showMessage("VALUE")'"; } } ?> > // end of body start tag <form action="login.php" method="POST"> Username <input type="text" size="30" name="txtUsername" id="user" /><br /> Password <input type="password" size="30" name="txtPassword" id="pass" /><br /> <input type="submit" value="Login" name="loginSubmit"/> </form> </body> <script type="text/javascript"> function showMessage(value) { document.getElementById(value).innerHTML= value+"can not be empty."; } </script>
If the verification is successful, the php code will not be an echo, and the javascript function will not be called. Works for me :). Tell me if this helps.
Riste source share