Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call javascript function in php code?

on my login form, i am putting server side validations and if error occurs i want to display those error just below the validated control. Now for this, i am trying to call javascript function to show validation message in php code but not able to call.

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if($_POST['txtUsername']=='')
{
    //here i want to call javascript function to display message    
}
}
?>
 <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>


<script type="text/javascript">

   function showMessage(value)
   {
    document.getElementById(value).innerHTML= value+"can not be empty."; 
   }
   </script>

Please tell me how to display server side validation just below the validated control in form.

like image 548
Kalpana Dixit Avatar asked Feb 20 '13 10:02

Kalpana Dixit


1 Answers

use this

if($_POST['txtUsername']=='')
{
     echo '<script> showMessage("txtUsername"); </script>';
}
like image 87
Yogesh Suthar Avatar answered Oct 18 '22 19:10

Yogesh Suthar